Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

405
Views
Node JS - Asynchronous when I want to display a list from Mongoose

I have a problem when I want to display a list of data from Mongoose (MongoDB) to my front-end in ejs

For example:

var User = require("./models/user");
var Houses = require("./models/house");

app.get("/", function(req, res){
    var list_houses = [];
    for(var i=0; i<req.user.city.length; i++)
    {
        Houses.find({'city': req.user.city[i]}, function(err, result){
            if(err)console.log(err);
            if(result){
                list_houses = list_houses.concat(result);//add array result to array list_prices
            }
        }); 
    }
    res.render("/prices_houses",{list_houses: list_houses});
});

I found the module 'async', but neither does not work, my code was:

var User = require("./models/user");
var Houses = require("./models/house");
var async = require('async');

app.get("/", function(req,res){
    var list_houses = [];
    async.waterfall([
        async.each(req.user.city, function(result, callback){
            Houses.find({'city': result}, function(err, result){
                if(err)console.log(err);
                if(result){
                    list_houses = list_houses.concat(result);
                }
            });
        });
    ], function(arg1, callback){
        res.render("/prices_houses", {list_houses: list_houses});
    });
});

When I want to display 'list_houses.length' in ejs, it shows this: 0

<%=list_houses.length%>

My schemas in mongoose are:

var User = new mongoose.Schema({
    city : [String] 
    ...
});

var House = new mongoose.Schema({
    city: String
    price: Double
});

I know that the problem is with Asynchronous, but I don't know how to solve it. Please help me with this problem.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You're overcomplicating it, I think. Seems like you can just use the $in operator for this. Something along these lines:

Houses.find({'city': {$in: req.user.city}}, function(err, results){
    if(err)console.log(err);
    console.log(results); // this should be an array of all houses in all cities that the user has in their city array
});
over 4 years ago · Santiago Trujillo Report

0

Yes the problem with is asynchronous nature of nodejs. You can not use for loop as it will be over before any of the databse calls gave any result. As Paul pointed out, you should use queries provided by mongo when you use array. But this is a common pattern you can use if you need to do multiple databases calls.

var User = require("./models/user");
var Houses = require("./models/house");

    app.get("/", function(req, res){
    var list_houses = [];
    var index = req.user.city.length;

    function findHouses(i, arr, cb){
       //databases call here

       Houses.find({'city': arr.city[i]}, function(err, result){
        if(err)console.log(err);
        if(result){
            cb(result);
        } else{
           cb(null);
        }
    }); 
    }


    findHouses(index--, req.user.city, function(result){
        if(result && index>=0){
            list_houses = list_houses.concat(result);
            findHouses(index--, req.user.city);
        } else if(index == 0){
            res.render("/prices_houses",{list_houses: list_houses});
        }

    });
});
over 4 years ago · Santiago Trujillo Report

0

this code should work fine i have used call back

app.get("/", function(req, res){
function listhouses(cb){
var list_houses = [];
for(var i=0; i<req.user.city.length; i++)
{
    Houses.find({'city': req.user.city[i]}, function(err, result){
        if(err)console.log(err);
        if(result){
            list_houses = list_houses.concat(result);//add array result to array list_prices
        }
    }); 
}
cb(list_houses);
}
listhouses(function(list_houses){
                res.render("/prices_houses",{list_houses: list_houses});   
                        });
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!