I am new to nodeJs but I already make research in order to solve my issue but it seems that I lacking a bit in async functionalities. I have found this solution but it does not work which I will post it down below : enter link description here
Here is my code
router.put("/placeOrder", [auth, isVerified, isExists], async (req, res) => {
try {
const productList = req.body; // body
await Promise.all(
productList.map(async (element) => {
// compare between the inserted element and the store one here
let getItemPrice = await Order.findOne({
_id: element._id,
buyerId: req.user._id,
orderStatus: "PENDING",
});
if (!getItemPrice) {
return res.status(400).json({
status: "failed",
message: "order not exists",
});
}
getItemPrice.items.map(async (item) => {
await Order.findOneAndUpdate(
{
"items.productId": item.productId,
},
{
$set: {
orderStatus: "ACTIVE",
"items.$.itemStatus": "ACTIVE",
paymentMethod: element.paymentMethod,
},
}
);
});
})
);
res.status(200).json({
message: "Order has placed",
});
} catch (error) {
return res.status(400).json({
status: "failed",
message: error.message,
});
}
// update documents
});
I am trying to check if the Id does not exist within my array if not exists just through an error that Id does not exist, it is working fine but it keeps print error to the logs that say: TypeError: Cannot create property 'Symbol(level)' on string 'Cannot set headers after they are sent to the client' Thanks for your time and collerations