简体   繁体   中英

Querying other collection inside forEach for Mongo DB and Node js

I am facing issue when I call mongoDB find() query in side another find query Here I am notable to send response value to postman but its showing in my console:

email = req.body.email;
role = req.body.role;
console.log("id",email)
let result = db.collection('deliveryBoy').find({ 
    DeliveryBoyEmail: email,
    DeliveryBoyrole: role
}).toArray();
var arr=[]
result.forEach((docs) => {
    db.collection('consumers').find({
        _id: new MongoClient.ObjectID(docs.consumerId)
    })
    .forEach((v)=>{
        v['Customer_name'] = v.name;
        arr.push(v)
    }) 
})
console.log(arr)
res.send(JSON.stringify(arr));

use await before calling find method and you can return res.json(arr) instead of calling res.send(JSON.stringify(arr))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM