0

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?

sinusGob
  • 4,053
  • 12
  • 46
  • 82

0 Answers0