Reputation: 585
I use multi find() to populate 'categories' and 'pic' for 'post'. But I don't know Where is the data returned finally in full to 'res.send(posts)'. Or use another method, 'Promise.all' for example, Please help me solve the problem
Post.find().then(posts=> {
async.forEach(posts, function(post, done) {
Cat.find().where('posts').in([post.id]).then(categories=> {
post.categories = categories;
var id=mongoose.Types.ObjectId(post.id);
File.findOne({'related.ref': id}).then(pic=>{
post.pic=pic;
});
})
//res.send(posts);????
});
});
Upvotes: 0
Views: 37
Reputation: 12071
You could use async-await for your route handler:
async (req, res) => {
const posts = await Post.find()
for (post of posts) {
const categories = await Cat.find().where('posts').in([ post.id ])
post.categories = categories
const id = mongoose.Types.ObjectId(post.id)
const pic = await File.findOne({ 'related.ref': id })
post.pic = pic
}
res.send(posts)
}
Upvotes: 1