const getGroup = async (req, res) => {
const { id } = req.params
try {
const group = await Group.findOne({ _id: id });
const groupNotesPromise = await group.group_notes.map( async (fileId) => {
const file = await File.findById(fileId)
return file;
})
const groupNotes = await groupNotesPromise.map( async (notePromise) => {
await notePromise.then(res => console.log(res))
})
console.log(groupNotes)
} catch (error) {
console.log(err.message);
res.status(400).json({ err: err.message });
}
}
Hello guys, I have a function above, this is retrieving data from MongoDB.
However, I would like to know why would my console.log(groupNotes)
be printed before the execution of the groupNotesPromise.map
above, as it has an await
in the function.
Below is the console.log(groupNotes)
and console.log(res)
from the code above
[ Promise { <pending> }, Promise { <pending> } ]
{
_id: new ObjectId("6496c645bf2bb491b931f082"),
fileName: 'test',
fileAwsReference: 'b4b71d06828d23d857b3611c2ce2480cf704f1fc1b9cb1879b563159631d162a',
createdBy: new ObjectId("648bab3c8044efccc461ad1d"),
createdIn: new ObjectId("648d7776284835591f536552"),
__v: 0
},
{
_id: new ObjectId("6496c9fafb91e60683fc812d"),
fileName: 'waeewaea',
fileAwsReference: '0e1f17f127f9a12a9e27d765bda1436297f0d58eb7d20f3e9dc8d39e64b58c5b',
createdBy: new ObjectId("648bab3c8044efccc461ad1d"),
createdIn: new ObjectId("648d7776284835591f536552"),
__v: 0
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/IjapmJf
Comments
Post a Comment