2017-02-25 65 views
0

嗨我有一個問題,運行一個循環,並獲取返回數據使用Promises解決不工作在循環Node.js

我有一個getStudentMarks從主題明智的數據庫中獲取學生分數的方法。

getStudentMarks: function(studentId, studentStandard) { 
    console.log("getStudentMarks invoked..."); 
    return new Promise(function(resolve, reject) { 
     r.table('student_subjects').filter({ 
      "studentId": studentId, 
      "studentStandard": studentStandard 
     }).pluck("subjectId", "subjectName").run(connection, function(err, cursor) { 
      if (err) { 
       throw err; 
       reject(err); 
      } else { 
       cursor.toArray(function(err, result) { 
        if (err) { 
         throw err 
        } else { 
         console.log(result.length); 
         if (result.length > 0) { 
          studentSubjectArray = result; 

          var studentMarksSubjectWiseArray = []; 
          studentSubjectArray.forEach(function(elementPhoto) { 
           r.table('student_marks').filter({ 
            "studentId": studentId, 
            "subjectId": studentSubjectArray.subjectId 
           }).run(connection, function(err, cursor) { 
            if (err) { 
             throw err; 
             reject(err); 
            } else { 
             cursor.toArray(function(err, result_marks) { 
              var studnetMarksDataObject = { 
               subjectId: studentSubjectArray.subjectId, 
               subjectName: studentSubjectArray.subjectName, 
               marks: result.marks 
              }; 
              studentMarksSubjectWiseArray.push(studnetMarksDataObject); 
             }); 
            } 
           }); 
          }); 
          resolve(studentMarksSubjectWiseArray); 
         } 
        } 
       }); 
      } 
     }); 
    }); 
} 

我被調用的方法,

app.post('/getStudentMarks', function(req, reqs) { 
    ubm.getStudentMarks(req.body.studentId, req.body.studentStandard) 
     .then((data) => { 
      console.log('return data: ' + data); 
     }) 
     .catch((err) => { 
      console.log(err); 
     }); 
}); 

當我運行代碼的工作精絕沒有錯誤。我獲得了studentMarksSubjectWiseArray陣列中的所有學生標記對象。但問題是,即使在studentSubjectArray循環完成之前,解決方案正在執行,我得到一個空數組作爲返回。我如何解決這個問題。我知道我沒有做Promises的權利。我是Promises的新手,所以我無法找出正確的方法。

+0

注意,相似的回報,執行你的範圍後扔停止:'不會發生reject'。 –

+0

@ L.Meyer我沒有得到你。我認爲問題出在我的循環上。 –

回答

0

發生這種情況是因爲在您的studentSubjectArray.forEach語句中,您執行一組異步操作r.table(...).filter(...).run(),並將其結果推送到數組中。但是,在執行resolve()後,這些操作完成,因此studentMarksSubjectWiseArray仍爲空。在這種情況下,您將不得不使用Promise.all()方法。

let promisesArray = []; 
studentSubjectArray.forEach((elementPhoto) => { 
    let singlePromise = new Promise((resolve, reject) => { 
     // here perform asynchronous operation and do the resolve with single result like r.table(...).filter(...).run() 
     // in the end you would perform resolve(studentMarksDataObject) 

     r.table('student_marks').filter({ 
      "studentId": studentId, 
      "subjectId": studentSubjectArray.subjectId 
     }).run(connection, function(err, cursor) { 
      if (err) { 
       throw err; 
       reject(err); 
      } else { 
       cursor.toArray(function(err, result_marks) { 
        var studnetMarksDataObject = { 
         subjectId: studentSubjectArray.subjectId, 
         subjectName: studentSubjectArray.subjectName, 
         marks: result.marks 
        }; 
        resolve(studnetMarksDataObject); 
       }); 
      } 
     }); 
    }); 
    promisesArray.push(singlePromise) 
}); 

Promise.all(promisesArray).then((result) => { 
    // here the result would be an array of results from previously performed set of asynchronous operations 
}); 
+0

我讀過它。但是我如何在我的代碼中應用這些我沒有得到的東西。如果你可以提供一個片段,這對我很有幫助。 –

+0

我剛剛更新了答案,以顯示如何使用'Promise.all'完成這個工作。 – piotrbienias

+0

謝謝。讓我試試這些代碼,儘快回覆你。 –