exports.getAllProducts = async () => {
const query = "some mysql query";
products = await db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT });
var result = new Array();
products.forEach(product => {
result.push({
//'isLiked': helpers.isLiked(req.user.id, product.productId),
'productId': product.productId,
'product_price': product.product_price,
'product_name': product.product_name,
'shop_name': product.shop_name,
'shop_picture': product.shop_picture,
'product_image': product.product_image,
'num_comments': product.num_comments,
})
});
return result;
}
In my Node.js project I am using Sequelize as an ORM. But sometimes I want to use raw queries but I have got this issue to fix. Once the query is done I am looping through it using a forEach function. For each iteration I want to call another async function (check the commented code) and put the returned value in isLiked as value and once it does all looping - send back the array as a response.
NOTE: the async function is another DB function which checks the DB for some value.
The problem Here I guess is since I am calling an async function and the loop doesn't wait until the function goes and gets result back and put in the value.