如何使用.map synchronic返回新数组?

时间:2017-12-21 12:14:52

标签: javascript node.js

我正在尝试在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);

});

1 个答案:

答案 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)
   }
})

You can also check this link