1

This question is already asked, but that not solve my issue.

In my Node.Js project i want to user SELECT Query inside forEach but that not working properly.

I refer some blog they say use async and await i tried but that also not working properly.

This is my code:

db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT} ).then(async dataList=>{
    let cubbersIdList = [];
    // dataList = [{"cubbersId": 27},{"cubbersId": 28},{"cubbersId": 29}]
    await dataList.forEach((value, key)=>{
        CubbersShopsData.findAndCountAll({where:{cubbersId:value.cubbersId}}).then(datas=>{
            cubbersIdList.push(datas);
            console.log("---data---");
        }).catch(error=>{
            logger.error(error);
            res.status(200).send({status: 'error', resCode:403, msg:'Internal Server Error...!', data:error});
        });
    });
    console.log("---data---");
    res.send(cubbersIdList); // get result here
    console.log(cubbersIdList);
});
Ramesh S
  • 585
  • 3
  • 10
  • 32
  • What do you mean by "not working properly" ? Could you be more precise on the expected result ? And the produced error ? By the way, add a return before "res.status..." when error occurs – Flo May 10 '19 at 10:19
  • @Flo: await not working, i mean first run this code `res.send(cubbersIdList);` – Ramesh S May 10 '19 at 10:21
  • 2
    Possible duplicate of [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – TGrif May 10 '19 at 10:22
  • @TGrif: No this is not duplicate – Ramesh S May 10 '19 at 10:24

2 Answers2

1

try this install npm

npm install async-foreach --save

add in your file

var forEach = require('async-foreach').forEach;

use forEach like this

db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT} ).then(async dataList=>{
let cubbersIdList = [];
// dataList = [{"cubbersId": 27},{"cubbersId": 28},{"cubbersId": 29}]
forEach(dataList,function(value, key){
    var done = this.async();
    CubbersShopsData.findAndCountAll({where:{cubbersId:value.cubbersId}}).then(datas=>{
        cubbersIdList.push(datas);
        done();
    }).catch(error=>{
        done();
        logger.error(error);
        res.status(200).send({status: 'error', resCode:403, msg:'Internal Server Error...!', data:error});
    });
},function(err){
    res.send(cubbersIdList); // get result here
    console.log(cubbersIdList);
})

});

rajpoot rehan
  • 435
  • 5
  • 14
1

Try to refactor your code like this to use async-await properly.

Call the asynchronous code in the for...of loop and await the promise and resolve the response and push it into your array later.

db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT})
.then(async dataList => {
    let cubbersIdList = [];

    for (const data of dataList) {
        const count = await CubbersShopsData.findAndCountAll({where: { cubbersId: data.cubbersId }});
        cubbersIdList.push(count);
    }

    return res.status(200).json(cubbersIdList); 
})
.catch(err => res.status(500).json({ message: 'Some error occured!' }));
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53