如何直接在mongoose方法中更新对象?
const item = new Schema({
timeleft: { type: Number, default: 24}
});
ItemSchema.methods.calculateTime = function() { // Done check it one hour
this.timeleft - 3;
this.save(); // doesn't work
}
app.get('/getItem/', function(req, res, next) {
Item.findOne({ name: "name"}, function(err, foundItem) {
foundItem.calculateTime(); // doesn't work
res.json(foundItem);
});
});
我试过这个版本,也没有用
ItemSchema.methods.calculateTime = function() { // Done check it one hour
this.timeleft - 3;
}
app.get('/getItem/', function(req, res, next) {
Item.findOne({ name: "name"}, function(err, foundItem) {
foundItem.calculateTime(); // doesn't work
foundItem.save(function(err, item) {
res.json(item);
});
});
});
如何使用mongoose方法更新它?