Reputation: 19
I've this mongoose Schema:
var UrlSchema = new mongoose.Schema({
description: String
});
Then, I create a model istance:
var newUrl = new Url({
"description": "test"
});
newUrl.save(function (err, doc) {
if (err) console.log(err);
else{
Url.update({_id: doc._id},{description: "a"});
}
});
But any update performed... Why? Thanks
Upvotes: 1
Views: 3030
Reputation: 10481
You need to add a callback to the update method or call #exec()
to perform the update:
var newUrl = new Url({
"description": "test"
});
newUrl.save(function (err, doc) {
if (err) console.log(err);
else{
Url.update({_id: doc._id},{description: "a"}, function (err, numAffected) {
// numAffected should be 1
});
// --OR--
Url.update({_id: doc._id},{description: "a"}).exec();
}
});
Just FYI: I personally stay away from update
because it bypasses defaults, setters, middleware, validation, etc which is the main reason to use an ODM like mongoose anyway. I only use update
when dealing with private data (no user input) and auto incrementing values. I would rewrite as this:
var newUrl = new URL({
"description": "test"
});
newUrl.save(function(err, doc, numAffected) {
if (err) console.log(err);
else {
doc.set('description', 'a');
doc.save();
}
});
Upvotes: 2