I'm looking to push object from a forEarch, after getting data inside forEarch, the console.log show me correct value and object are properly pushed to the array.
But at the end of the forEach, if I try to access to the object pushed, there is no object inside the array
Code below
async function get_line_items(items) {
line_items2 = [];
await items.forEach((element, index) => {
const id = element.id;
if (id) {
await getPriceFromCartCheckout(id, (err, results) => {
if (err) {
console.log(err);
return;
}
if (results && !isEmpty(results)) {
// Add new item
line_items2.push({
name: element.name,
amount: results[0].price,
currency: "eur",
quantity: element.quantity,
})
}
console.log(line_items2) // => Here objects are properly added to the array on each loop
});
}
})
console.log(line_items2) // => Here the array is empty.
return line_items2;
}
const line_items = await get_line_items(req.body[1].items);
console.log( line_items); // => Here the array is empty.
module.exports = {
getPriceFromCartCheckout: async (id) => {
return await pool.query(`SELECT volume, price FROM products WHERE id = ?`, [
parseInt(id)
])
.then(iuidtest => {
return iuidtest;
});
},
};
Any help would be appreciate :)
Found the solution here :
NodeJS async MySQL call to connection pool returns no result