Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

412
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda