如何.extend()mongoose集合中的对象(与node.js一起)

时间:2014-07-03 14:47:54

标签: node.js mongodb mongoose

我的架构如下。

var TeamSchema = new Schema({
    // Team Name.
    name: String,
    lead: String,
    member :{
      block : Number,
      status : String,
      prevGames :[{
       gameRef :{
        type: Schema.ObjectId,
        ref: 'Game'
    },
       year : Number
      }],
      memberId : String,
      student : {
        type: Schema.ObjectId,
        ref: 'Student'
    }
});

当我尝试保存team.member对象时,我可以保存所有其他值,除了' student'。这是我的Server API函数,如下所示。

exports.saveMember = function(req, res, next) {
    console.log("Inside save member");
    console.log("Request body");
    console.log(req.body);

    var id = req.team.id;
    Team
        .findOne({
            _id: id
        })
        .populate('member.student member.prevGames.gameRef')
        .exec(function(err, curTeam) {
            if (err) return next(err);
            if (!curTeam) return next(new Error('Failed to load curTeam ' + id));
            // Found the team!!
            console.log("RESULT curTeam DETAILS");
            console.log(req.body);
            console.log("curTeam member empty");
            console.log(curTeam.member);             
           curTeam.member = req.body; 
            console.log("curTeam re-assigned");          
            console.log(curTeam.member); 
            curTeam =_.extend(curTeam,req.body.student); 
            console.log("curTeam member added student");
            console.log(curTeam);               
            /*curTeam.save(function(err) {
                if (err) {

                    res.send(500, {
                        message: "Unable to update curTeam: " + err
                    });

                } else {
                    res.send(200, {
                        message: "OK"
                    });

                }
            });*/
        });
};

我评论了保存。 我的控制台输出:

  console.log("RESULT curTeam DETAILS");
                // All the member details properly come into the req object.
  console.log("curTeam member empty");
               // the team coming as a result of find. does not contain student object. 
               member ={prevGames[]}

因为没有学生'对象,我试过,扩展学生对象。语法显示没有错误。但结果我根本看不到学生的对象。

我的对象延伸是否恰当。如果没有,请纠正我。感谢。

0 个答案:

没有答案