1

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.

alexandrenriq
  • 23
  • 1
  • 6

3 Answers3

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
});
Paul
  • 35,689
  • 11
  • 93
  • 122
1

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});
        }

    });
});
Anurag Awasthi
  • 6,115
  • 2
  • 18
  • 32
  • Thank you man! But, how it work the function findhouses? And what are those variables i, arr, and cb. I am noob in node.js and mongodb yet. Thanks for you suppor! – alexandrenriq Jan 13 '17 at 06:20
1

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});   
                        });
});
punith bp
  • 174
  • 1
  • 12
  • Thank you!! But, how cb work in the function listhouses? It is some of node? Because I don't know it – alexandrenriq Jan 13 '17 at 06:16
  • Take a look at this link about call backs it will be help full in node.js you are welcome : (http://stackoverflow.com/a/19739852/6269843) – punith bp Jan 13 '17 at 06:25