How do I update an object in mongoose methods directly?
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);
});
});
I tried this version, doesn't work as well
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);
});
});
});
How do I update it using mongoose method?