在调用mongoose的静态方法时我收到此错误并且我已经搜索了该错误但仍无法找到相关解决方案来解决它
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'returnEventType'
型号:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var portalSchema = new Schema({
created: {
type: Date,
default: Date.now()
}
}),
eventType = new Schema({
ID: {
type: Schema.Types.ObjectId,
ref: 'docevents'
},
Accepted: {
type: Boolean,
default: 0
}
});
var Portal = mongoose.model('Portal', portalSchema),
EVENT = Portal.discriminator('EVENT', eventType);
portalSchema.statics.returnEventType = function(cb) {
cb(EVENT);
};
控制器:
exports.sendInvite = function(req,res) {
Portal.returnEventType(function(Event){
var EventObj = new Event({'ID': req.user._id});
EventObj.save(function(err,eventObj) {
console.log(eventObj);
});
}
答案 0 :(得分:2)
在模型创建后,您无法向模型中添加静态方法,因此在调用returnEventType
之前移动model
的定义:
portalSchema.statics.returnEventType = function(cb) {
cb(EVENT);
};
var Portal = mongoose.model('Portal', portalSchema);