0

I am unable to call mongoDB callback function inside the loop. I am providing my array and code below.

[
    {
        "location": "NEW DELHI",
        "nos_of_fos": 15,
        "login_id": [
            "9619300317",
            "9619300343",
            "9619300338",
            "9619300351",
            "9619300322",
            "9619300316",
            "9619300323",
            "9619300328",
            "9619300341",
            "9619300309",
            "9619300310",
            "9619300329",
            "9619300353",
            "9619300356",
            "[email protected]"
        ],

    },
    {
        "location": "North West Delhi",
        "nos_of_fos": 6,
        "login_id": [
            "9619300355"
        ],

    }
]

The above is my input array.

finalOut.forEach(function(listItem, index){
                    var remarkCount=0;
                    console.log('items',listItem['login_id']);
                    listItem['login_id'].forEach(function(item, index1){
                        Feedback.collection.countDocuments({login_id:item},function(cerr,cdocs){
                            if (!err) {
                                if (docs >0) {
                                    console.log('docsinner',cdocs);
                                    remarkCount+=parseInt(cdocs);
                                }
                            }
                        })
                    })
                    console.log('docsout',remarkCount);
                    finalOut[index]['total_remarks']=remarkCount;
                })

Here before finishing mongoDB checking for first iteration loop getting finished. Here I need to check one by one login_id value and only after finishing the mongoDB task new count will assign.

7
  • use promises instead Commented Jan 25, 2019 at 9:37
  • Can you update you answer. Commented Jan 25, 2019 at 9:38
  • please try this Commented Jan 25, 2019 at 9:46
  • @AshokMandal : No,its not working as expected. Commented Jan 25, 2019 at 10:02
  • @AshokMandal: you can check it.there is no total_remarks value there. and also no console message are executing. Commented Jan 25, 2019 at 10:02

1 Answer 1

1
finalOut.forEach(function(listItem, index) {
  var remarkCount = 0;

  var promises = listItem['login_id'].map(function(item, index1) {
    return new Promise((resolve, reject) => {
      Feedback.collection.countDocuments({ login_id: item }, function(
        err,
        docs
      ) {
        if (!err) {
          if (docs > 0) {
            console.log('docsinner', docs);
            remarkCount += parseInt(docs);
          }
          resolve();
        }
        reject();
      });
    });
  });
  Promise.all(promises)
    .then(() => {
      finalOut[index]['total_remarks'] = remarkCount;
    })
    .catch(err => {});
});
Sign up to request clarification or add additional context in comments.

1 Comment

there is no total_remarks value in the final array.

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.