1
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.

1 Answer 1

1

In your case, if you want to perform an asyncronous operation in sequence, you cannot use a forEach loop. Instead, you should use for ... of which will allow you to use async/await properly:

exports.getAllProducts = async () => {
  const query = 'some mysql query';

  const products = await db.sequelize.query(query, {
    type: sequelize.QueryTypes.SELECT
  });
  var result = new Array();

  for (product of products) {
    const isLiked = await helpers.isLiked(req.user.id, product.productId)
    result.push({
      'isLiked': isLiked,
      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;
};
Sign up to request clarification or add additional context in comments.

3 Comments

hi, that works perfectly well. however if use this same async helper functions in another query and from the front-end if i want to call both the api endpoints at the same time. it blows up. any solution for this?
now i ran into this problem and i am very much desperate in solving this one. stackoverflow.com/questions/53483697/…
@bee321 I'll take a look. If this answer is correct for the question, could you accept it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.