Here, productIds has ID of all ordered items in an array like ["1294382", "2913892"]
So I'm trying to get all the items with those ID's from MongoDB's database. I'm mapping through each of them, and store them in orderedItems array. So that i can access price (or any property) of the item in database.
productRouter.post("/", async (req, res) => {
const productIds = req.body.orderedItems.map((item) => item.id);
let orderedItems = [];
productIds.map(async (productId) => {
const d = await Product.findById(productId);
orderedItems.push(d);
});
const unusedVariable = await Product.findById("61...da1"); //If i delete that line (or only await), orderedItems returns empty array.
console.log(orderedItems);
});
Now, expected output is:
[
{
_id: new ObjectId("614632cc8aa9513567dfbca4"),
name: 'ürün1',
desc: 'ürün1 açıklama',
price: 50,
__v: 0
},
{
_id: new ObjectId("614632cc8aa9513567dfbca2"),
name: 'kartal kupa',
desc: 'kartal resimli kupa',
price: 50,
__v: 0
}
]
And I get that output, but only if I have the unusedVariable line with await. If i remove that line, or remove "await" from there, the output i get is: [] . So an empty array instead of filled with objects.
Why ? And how do i solve that (just leave unusuedVariable there?) ? How bad is this code ?