admin管理员组

文章数量:1323529

I am trying to learn CRUD with mongoose. I am only missing the update part. What am I doing wrong?

MY MODEL

var mongoose = require('mongoose');

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

mongoose.model('TestData', testSchema);

MY ROUTES

// get the models
var Test = mongoose.model('TestData');

PARAM
If the link has the 'test' as a url parameter, it will look if the object exist in the database, else return errors.

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();
    });
});

GET BY ID

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

DELETE

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

MY PROBLEM
Now here es my problem, if I try to update one, I am simply missing something.

/* 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);
    });                         
});

I am not getting any errors, but it is neither updating anything. It is even returning some data.

{
    "ok": 0,
    "n": 0,
    "nModified": 0
}

I am trying to learn CRUD with mongoose. I am only missing the update part. What am I doing wrong?

MY MODEL

var mongoose = require('mongoose');

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

mongoose.model('TestData', testSchema);

MY ROUTES

// get the models
var Test = mongoose.model('TestData');

PARAM
If the link has the 'test' as a url parameter, it will look if the object exist in the database, else return errors.

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();
    });
});

GET BY ID

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

DELETE

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

MY PROBLEM
Now here es my problem, if I try to update one, I am simply missing something.

/* 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);
    });                         
});

I am not getting any errors, but it is neither updating anything. It is even returning some data.

{
    "ok": 0,
    "n": 0,
    "nModified": 0
}
Share Improve this question asked May 4, 2015 at 13:37 SigilsSigils 2,5728 gold badges26 silver badges37 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Try without unneeded first argument (test), because the req.test is an instance of the Test model.

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

The update method will not return object, and if you want to return object then use save:

req.test.name = 'new data';
req.test.save(function(err, test){
  res.json(test);
});

Your problem is in that both the remove and the update methods, don't work on a model instance but require queries, like

{_id:id}

You update statement isn't matching anything, and I bet your remove endpoint won't work either.

If you want to operate on the live model instance on req.test then set the value on test directly and save the model:

req.test.name= 'new data';
req.test.save(...

Deleting a model is always going to require a new query as you can't delete an instance directly.

本文标签: javascriptUpdating with mongoosenot returning any errors but neither updatingStack Overflow