I have a code that after find and populate, I will then add a new key ('order_date') based on createdAt.
const summaryGetOrders = async (start_date, end_date) => {
let orders = await Order.find({ createdAt: { $gte: new Date(start_date), $lt: new Date(end_date) } }).populate(
'orderdetails_id'
);
await Promise.all(
orders.map(async (i, index) => {
i.order_date = i.createdAt;
// orders[index] = i
// orders[index].order_date = i.createdAt
})
);
console.log(orders);
return orders;
};
I have checked with console.log(i.createdAt), it do have the date element in the i, but when I do console.log(orders), it is not retunring me order_date.
Is it caused by the async await function? I am doing this because my express boilerplate automatically removes the createdAt key via the toJSON function on my model. I wish not to modify because my code is tailor not to deal with createdAt.