0

我有一个问题,MongoosefindByIdAndUpdate没有在回调中返回正确的模型。

我想TIMELINE.Description从用户文档更新属性:

var refereeSchema = mongoose.Schema({
    first_name: String,
    last_name: String,
    email: String,
    phone: Number,
    age: Number,
    role: String,

    note: Number,
    favorite_teams: String,
    hometown: String,
    picture: String,
    timeline: 
        {
            id_Game1: String,
            date_game: Date,
            description: String
        }

})  ; 

// new timeline
router.put('/:id', function(req,res,next){
      models.users.findByIdAndUpdate(req.params.id,{$set: {description : req.body.description}}, {new:true} , function(err,user){
       if(err){
           res.json({error :err}) ; 
       } else{
           res.send(user) ; 
       }
    });
});
4

1 回答 1

1

您缺少timeline要更新的子文档键部分。它应该是:

router.put('/:id', function(req,res,next){
    models.users.findByIdAndUpdate(
        req.params.id,
        {$set: {'timeline.description': req.body.description}}, 
        {new: true},
        function(err,user){
            if(err){
                res.json({error :err}) ; 
            } else{
                res.send(user) ; 
            }
        });
});
于 2016-04-12T16:37:33.663 回答