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
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