0

我在一个集合中有一个 ObjectId 的引用数组。我想在 json 响应中返回与这些 Id 链接的对象数据。

retrieveFromUser: function( req, res ) {
    var user_id =   req.params.user_id;
    User.findById( user_id, function( err, user ) {
        if( err ) {
            res.send( 404, "Unable to find user");
        } else {
            // This returns the array but I want the objects data
            return res.json( user.constructions );
        }
    });
}

我怎样才能做到这一点 ?
多谢 !

4

1 回答 1

2

尝试使用populate()方法

retrieveFromUser: function( req, res ) {
 var user_id =   req.params.user_id;
    User.findOne({ _id: user_id }).populate('constructions').exec(function( err, user ) {
        if( err ) {
            res.send( 404, "Unable to find user");
        } else {
            // This returns the array but I want the objects data
            return res.json( user.constructions );
        }
    });
}
于 2013-10-10T08:40:22.900 回答