使用mongoose更新,不返回任何错误但不更新

Sig*_*ils 7 javascript mongoose node.js

我正在尝试用猫鼬学习CRUD.我只是错过了更新部分.我究竟做错了什么?

我的模特

var mongoose = require('mongoose');

var testSchema = new mongoose.Schema({
    name: String,
    number: Number
});

mongoose.model('TestData', testSchema);
Run Code Online (Sandbox Code Playgroud)

我的路线

// get the models
var Test = mongoose.model('TestData');
Run Code Online (Sandbox Code Playgroud)

PARAM
如果链接具有'test'作为url参数,它将查看对象是否存在于数据库中,否则返回错误.

router.param('test', function(req, res, next, id){
    var query = Test.findById(id);

    query.exec(function(err, test){
        if(err){ return next(err); }
        if(!test){ return next(new Error('can\'t find test')); }
        req.test = test;
        return next();
    });
});
Run Code Online (Sandbox Code Playgroud)

GET BY ID

    /* GET testdata/:id */
    router.get('/testingdata/:test', function(req, res, next){
        req.test.populate('test', function(err, test){
            res.json(test);
        });
    });
Run Code Online (Sandbox Code Playgroud)

删除

/* DELETE testdata/:id */
router.delete('/testingdata/:test', function(req, res, next){
    req.test.remove('test', function(err, test){
        console.log('removed');
        res.json(test);
    });                         
});
Run Code Online (Sandbox Code Playgroud)

我的问题
现在我的问题来了,如果我尝试更新一个,我只是遗漏了一些东西.

/* PUT testdata/:id */
router.put('/testingdata/:test', function(req, res, next){
    req.test.update('test',{$set: {name: 'new data'}} , function(err, test){
        res.json(test);
    });                         
});
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误,但它既没有更新任何内容.它甚至返回了一些数据.

{
    "ok": 0,
    "n": 0,
    "nModified": 0
}
Run Code Online (Sandbox Code Playgroud)

Tal*_*kov 5

尝试不使用不需要的第一个参数 ( test),因为req.testTest模型的一个实例。

req.test.update({name: 'new data'} , function(err, test){
  res.json(test); // test would be `1` if success
});
Run Code Online (Sandbox Code Playgroud)

update方法不会返回对象,如果要返回对象,请使用save

req.test.name = 'new data';
req.test.save(function(err, test){
  res.json(test);
});
Run Code Online (Sandbox Code Playgroud)