承诺链:父承诺不会等到子承诺得到执行

时间:2020-02-07 04:27:26

标签: javascript node.js mongodb promise es6-promise

我从一个mongoDB集合中获取数据,在该响应中,我获得了另一个集合的数据ID,然后将其获取并合并到一个对象中。

这是我的代码,但不是等待unitl child primise被执行。

引导我输入代码错误。

 Courses.find({})
    .then( course => {
        //getting data from one collection
        let CoursePromises = course.map(
          key => {

            new Promise((resolve, reject) => {
                key.questions = []
                //getting data from another collection via Id fetched from first collection.
                let getQuestionsPromises = key.questionIds.map(
                  ques =>
                    new Promise((resolve, reject) => {
                             Questions.find({_id: ques._id})
                                .then(question => {
                                    resolve(question)
                                }).catch(err => {
                                    console.error("Error in  question ", err.message)
                                })
                    })
                )
                Promise.all(getQuestionsPromises).then((data) => {

                     key.questions.push(data)
                    console.log("getQuestionsPromises", key)
                })
                resolve(key)
            })
        })

        Promise.all(CoursePromises).then((data) => {
            console.log("CoursePromises") // here promise is now wait for exection done 
            res.send({ status: true, data: course })
          }
        ) 

我得到了这样的第一笔收藏回复:

{
    "status": true,
    "data": [
        {
            "_id": "5e3c1b683ac31f24da39e50a",
            "courseName": "Test",
            "duration": 1,
            "createdBy": "John Die",
            "__v": 0,
            "updatedAt": "2020-02-06T13:58:00.906Z",
            "createdAt": "2020-02-06T13:58:00.906Z",
            "isAssigned": false,
            "questions": []
            "questionIds": [
                {
                    "index": 1,
                    "_id": "5e3c1b683ac31f24da39e509"
                }
            ]
        }
    ]
}

使用questionIds,我获取了另一个记录,并将该响应放入现有对象中 像这样:

{
    "status": true,
    "data": [
        {
            "_id": "5e3c1b683ac31f24da39e50a",
            "courseName": "Test",
            "duration": 1,
            "createdBy": "John Die",
            "__v": 0,
            "updatedAt": "2020-02-06T13:58:00.906Z",
            "createdAt": "2020-02-06T13:58:00.906Z",
            "isAssigned": false,
            "questions": [
                [
                    [
                        {
                            "_id": "5e3c1b683ac31f24da39e509",
                            "index": 1,
                            "isVideo": false,
                            "questionType": "MCQ",
                            "question": "Is this a demo question?",
                            "title": "Question",
                            "description": "this is question description",
                            "link": "",
                            "createdBy": "Harsh",
                            "updatedBy": "",
                            "__v": 0,
                            "updatedAt": "2020-02-06T13:58:00.521Z",
                            "createdAt": "2020-02-06T13:58:00.521Z",
                            "options": [
                                {
                                    "one": "two"
                                }
                            ]
                        }
                    ]
                ]
            ],
            "questionIds": [
                {
                    "index": 1,
                    "_id": "5e3c1b683ac31f24da39e509"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

使用这种复杂结构时,应遵循纯Business_Id语法。还可以使用async-await.lean()从猫鼬对象转换为普通对象。

简化代码:

course