I am trying to get data from database I have an array in which collection of id are present and i try to get data from a database for each id and then send to a client if try something like this:-
router.get('/getcart1', async (req, res) => {
const email = req.body.email;`enter code here`
const data = await cart1.find({ email: email });
const id = data[0].id;
let details = await id.map(async (e) => {
console.log('start');
console.log(e);
let data = await books.find({ _id: e });
console.log('end');
return data;
});
res.json({ data: details });
});
the execution is start e start e end end ans when we send data details it show empty object i want all details.Inside a loop i have data but outside it show empty object
id.mapbut is it an array?await Promise.all(id.map ... )awaitan array of promises, you can only await a promise for an array. You're missing aPromise.allcall.JSON.stringify(await [Promise,Promise ... ])should not return an empty object. Or maybe he meant[{},{} ...]as an empty object