如何直接更新mongoose方法中的对象?

时间:2017-01-12 11:01:10

标签: node.js mongoose

如何直接在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方法更新它?

0 个答案:

没有答案