当我使用model.find()时,我正在使用mongoose并添加方法函数 并运行model.fnname(),它是未定义的
var mongoose = require('mongoose');
var timestamps = require('goodeggs-mongoose-timestamps');
var Schema = mongoose.Schema;
var OrderSchema=new Schema({
code:{
type: String,
required: true,
unique: true
},
percentage:{
type: String,
required:false,
},
});
OrderSchema.methods.updateProductionStatus = function updateProductionStatus() {
this.percentage="88%";
return;
}
OrderSchema.plugin(timestamps);
module.exports = mongoose.model('Order', OrderSchema);
控制器代码
let order=await Order.findOne();
console.log(order) //this is working
order.updateProductionStatus(); ///this is undefined
任何想法?
修改
发现上面的例子正在运行,但是
let orders=await Order.aggregate(........);
console.log(orders[0]) ///works
orders[0].updateProductionStatus() //isn't working