我如何在对象数组上运行mongoose方法

时间:2017-01-12 13:39:20

标签: node.js mongodb mongoose

我的目标是在一个对象数组上运行mongoose方法。例如

const ItemSchema = mongoose.Schema({
    name: String,
    time: { type: Number, default: 24 }
});

ItemSchema.methods.minus2 = function() {
    this.time -= 2;
    this.save();
}

Api方法

app.get('/allItems', function(req, res) {
    Item.find({}, function(err, items) {
      // I want to run items.calculateTime(); but it is not possible.
      // How would I run calculateTime function on the array of objects?
    });
});

0 个答案:

没有答案