Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

410
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda