我正在尝试在node.js中创建一些同步,我遇到了一些问题。
如何从newArr
获取每个项目将成为返回对象的数组then
?
Rating.find({userID: req.body.id}).then( (ratings) => {
var newArr = ratings.map( (rating) => {
User.find({_id: rating.reviewerID}).then( (user) => {
return {
firstName: user[0].firstName,
text: rating.text,
rating: rating.rating
}
})
})
console.log(newArr);
});
答案 0 :(得分:0)
您的方式不正确
Rating.aggregate([
{
$lookup: {
localField: "reviewerID",
from: "user",
foreignField: "_id",
as: "some_data"
},
},
], function(err, data) {
if (err) throw err
if (!data)return res.send('data not ');
return res.status(200).json(data)
}
})