1

not able to get items. it return [] . but it show correct on console.log(item). i think before my map() runs complete. it print all data. how to solve this issue. i am new in node.

function getBlockUsers() {
    return new Promise(function (resolve, reject) {

        BlockUser.find({userId:req.user._id}).populate("blockedId").lean().exec(function (err,result) {
            if(err){
                reject({"msg":"failed to getting block user."})
            }else{
                var results = [];
                result.map(function(item){
                    Vehicle.findOne({userId:item.blockedId}).lean().exec(function(err,vehicle){
                        if(vehicle){
                            item.vehicleId = vehicle._id;
                            item.vehicleModel = vehicle.model;
                        }
                        results.push(item)
                        console.log(item)
                    });
                });
                resolve(results);
            }


        })
    });
}
  • map returns a mapped item for each result. This looks like you need to just iterate over the result items. – hawkstrider Jun 19 '18 at 14:54
  • there is aysnc inside your map. https://stackoverflow.com/questions/33438158/best-way-to-call-an-async-function-within-map – feiiiiii Jun 19 '18 at 15:01

2 Answers2

0

The problem is you have non-blocking code inside your result.map().

You should try using just one DB query. Then resolve all the items in the exec callback. Otherwise use a promise for the original query.

Vehicle.find({ $in: { userId: result.map( item => item.blockedId) }}).lean().exec( (err, results) => {
    // add the vehicle / model ids to each item in results
    resolve(results)
})
JFord
  • 126
  • 1
  • 9
0

Because you use an async function in the map function wish is synchronous you need to create an array of promise and use Promise.all before the resolve to wait for all the results.

The code bellow should fix your issue.

function getBlockUsers() {
    return new Promise(function (resolve, reject) {

        BlockUser.find({userId:req.user._id}).populate("blockedId").lean().exec(function (err,result) {
            if(err){
                reject({"msg":"failed to getting block user."})
            }else{
                var results = result.map(function(item){
                    // don't forget to return in the map function
                    return new Promise(function (resolve1, reject1) {
                        Vehicle.findOne({userId:item.blockedId}).lean().exec(function(err,vehicle){
                            if (err) return reject1(err)
                            if(vehicle) {
                                item.vehicleId = vehicle._id;
                                item.vehicleModel = vehicle.model;
                            }
                            resolve1(item)
                        });
                    })
                });
                
                // here you wait all the promises of results
                resolve(Promise.all(results));
            }

        })
    });
}
NBeydon
  • 695
  • 6
  • 14