2017-11-27 7 views
0

약속을 사용하여 forEach 루프 다음에 값을 반환하는 방법을 알아야합니다. 이 순간에, 때 내 주를 시작, 내가 얻을 :약속을 사용하여 forEach 루프 다음에 값을 반환하는 방법은 무엇입니까?

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

(내 sampleidlist 만 2 개 레코드를 포함) 이 내 코드입니다 : 어떤 제안

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); 
    }) 
}) 

? 대단히 감사합니다.

+0

[비동기 호출의 응답을 반환하는 방법은 무엇입니까?] (https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous -call) – Liam

+1

'find_paz'에서 각 약속을 해결하거나 거절해야하며 약속 배열에서만'Pomise.all()'을 사용해야합니다 – charlietfl

답변

0

Promise.all과 Promise.resolve 사이에 혼란이 있습니다. 여기 :

반환 db.collection ('RUN'() { "IDRUN"query.idRun }를) 찾을 수 있습니다.. toArray() 그 때는 ((아웃) =>를 {

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); 
}) 

가 작동하지 않을 경우 내가 당신이 설명이 필요한 경우 알고 있거나하자, 한번 시도해

0

을 당신은 Promise.resolve()Promise.all() 잘못된 방법을 사용하는 당신은 단지 다음과 같이 다음 .then()Promise.all()를 호출해야합니다..

Promise.all(Promisearr).then(value => 
    console.log(value) 
)