'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.
Solution 1:[1]
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
});
Solution 2:[2]
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});
}
});
});
Solution 3:[3]
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});
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paul |
| Solution 2 | |
| Solution 3 |
