NodeJS callback after multiple async-functions in for-loop

后端 未结 2 515
谎友^
谎友^ 2021-01-24 10:47

I get a document from a mongodb which contains an array with comments for that document. In the comment is the _id of the user which wrote the comment.

I now need to ge

2条回答
  •  执念已碎
    2021-01-24 11:28

    You can try like the following way

    const d = {
        "_id" : ObjectId("5c18c1cbc47e5e29d42e4b0e"),
        "completed" : false,
        "completedAt" : null,
        "comment" : [ 
            {
                "_id" : ObjectId("5c18c95e328c8319ac07d817"),
                "comment" : "This is a comment",
                "rating" : [ ],
                "user" : ObjectId("5c18b76e73236d2168eda2b4")
            }, 
            {
                "_id" : ObjectId("5c18fb578de5741f20a4e2bd"),
                "comment" : "Another comment",
                "rating" : [ ],
                "user" : ObjectId("5c18b76e73236d2168eda2b4")
            }
        ]
    }
    
    d.comment.forEach( async (obj, index) => {
        await new Promise((res) => {
                obj.counter = index;
                res();
        })
    });
    
    console.log(d);
    

    For reference please take a look on following link Asycn/Await using forEach

提交回复
热议问题