I have a request that will return restaurant offers
console.log(offers)
inside the for loop, returns what am I want (array with objects) but when I send the offers to the client with res.send(offers)
return an empty array
what I should to do?
offers: async (req, res) => {
let orders = await req.models.order.find({
"customers.customer_id": req.user._id,
});
let cashbacks = await req.models.cash_backs
.find()
.populate("restaurant_id");
let offers = [];
for (let i = 0; i < cashbacks.length; i++) {
orders.forEach(async (order) => {
if (order.cash_back_id != cashbacks[i]._id) {
await req.models.order
.find({
"customers.customer_id": req.user._id,
cash_back_id: cashbacks[i]._id,
})
.then((result) => {
let allowCount = cashbacks[i].per_visit.values.length;
if (allowCount > result.length) {
offers = [...offers, cashbacks[i]];
console.log(offers); // return [{..}, {..}, {..}]
} else {
return;
}
});
}
});
}
res.send(offers);
},