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
2 Answers
Reset to default 5Try 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
版权声明:本文标题:javascript - Updating with mongoose, not returning any errors but neither updating - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129427a2422090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论