0

I need to know how to return a value after a forEach loop using Promises. In this moment, when I launch my main, I get :

[ Promise { <pending> }, Promise { <pending> } ]

(my sampleidlist contains only 2 records) This is my code :

MongoClient.connect("mongodb://127.0.0.1/myproject", function(err, db) {
  return db.collection('RUN').find({
      "idRun": query.idRun
    }).toArray()
    .then((out) => {

      var sampleidlist = out[0].SAMPLE_ID
      var pazlist = []
      // Promisearr is the array of promises where I try to push the promises
      var Promisearr = []
      // there is the function find_paz that return idPaz for every sampleId in sampleidlist                        
      function find_paz(sampleid) {
        // I return a new Promise for every sampleId
        // I want to create an array of idPaz 
        return new Promise((resolve, reject) => {
          db.collection('PATIENTS').find({
              "SAMPLE_ID": sampleid
            }).toArray()
            .then((pazArr) => {
              var singlepaz = []
              singlepaz.push(pazArr[0].idPaz)
              return singlepaz
            })
            .then((singlepaz) => {
              pazlist.push(singlepaz)

            })
        })
      }
      // Here the forEach loop
      sampleidlist.forEach(sampleid => {
        Promisearr.push(
          find_paz(sampleid)
        )
      })
      Promise.resolve(Promisearr)
        .then(Promise.all(Promisearr))
        .then(value => {
          // value return {promise<pending>}
          // I want that value is the array of idPaz
          console.log(value)
        }).catch((err) => {
          console.log('errored', err);
        })

    }).catch((err) => {
      console.log('errored', err);
    })
})

Any suggest? Thank you very much :)

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Ritulla
  • 33
  • 3
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Nov 27 '17 at 14:43
  • 1
    You need to resolve or reject each promise in `find_paz` and only use `Pomise.all()` on the array of promises – charlietfl Nov 27 '17 at 14:47

2 Answers2

0

You have it mixed up between Promise.all and Promise.resolve. Here:

return db.collection('RUN').find({ "idRun": query.idRun }).toArray() .then((out) => {

  var sampleidlist = out[0].SAMPLE_ID
  var pazlist = []

  var Promisearr = []

  function find_paz(sampleid) {

      return db.collection('PATIENTS').find({
          "SAMPLE_ID": sampleid
        }).toArray()
        .then((pazArr) => {
          var singlepaz = []
          singlepaz.push(pazArr[0].idPaz)
          return singlepaz
        })
        .then((singlepaz) => {
          pazlist.push(singlepaz)
          return;
        })
    })
  }
  Promise.all(sampleidlist.map(find_paz))
    .then(values => {

      //values is an array with all the promises resolved
      //pazlist should have your data.
    }).catch((err) => {
      console.log('errored', err);
    })

}).catch((err) => {
  console.log('errored', err);
})

Give it a try, let me know if you need clarification or if it doesn't work.

yBrodsky
  • 4,981
  • 3
  • 20
  • 31
0

You are using Promise.resolve() and Promise.all() the wrong way. You should just call Promise.all() then .then(), like this :

Promise.all(Promisearr).then(value =>
    console.log(value)
)
Fathy
  • 4,939
  • 1
  • 23
  • 25