I try to make a list with products that I send to the client, but the res.send gets executed before the loop has finished.
Do you have any suggestions? Async/await doesn't seem to work
Here is my code:
const Outfits = require("../models/Outfits");
const Products = require("../models/Products");
module.exports = (req, res) => {
Outfits.find({ outfitId: req.params.id }, async (error1, result) => {
if (error1) {
res.status(400).json({
status: "fail",
});
} else {
let productIds = result[0].productIds;
let productList = [];
for (let i = 0; i < productIds.length; i ) {
await Products.find({ id: productIds[i] })
.then((product) => {
console.log(product);
productList.push(product);
})
.catch((error2) => {
res.status(400).json({
status: "fail",
});
});
}
console.log(productList);
res.status(200).json({
status: "success",
body: productList,
});
}
});
};
Thank you very much!
CodePudding user response:
In order to have multiple asynchrone tasks executed at once, a clean way is to use a Promise.all
and pass an array of your asynchrone tasks. Like so :
const Outfits = require("../models/Outfits");
const Products = require("../models/Products");
module.exports = (req, res) => {
Outfits.find({ outfitId: req.params.id }, async (error1, result) => {
if (error1) {
res.status(400).json({
status: "fail",
});
} else {
let productIds = result[0].productIds;
//
const productList = await Promise.all(
productIds.map((id) => Products.findById(id))
);
console.log(productList);
res.status(200).json({
status: "success",
body: productList,
});
}
});
};
CodePudding user response:
You need to call await
on the Products.find
function like this
for (let i = 0; i < productIds.length; i ) {
try {
const product = await Products.find({ id: productIds[i] }).exec()
console.log(product);
productList.push(product);
} catch (error) {
res.status(400).json({
status: "fail",
});
}
}