2017-02-25 62 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

谢谢。让我试试这些代码,尽快回复你。 –