Mongoose statics Type Error : no such method
Mongoose statics Type Error : no such method
在调用 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);
});
}
您不能在模型创建后向其添加静态方法,因此请在调用 model
:
之前移动 returnEventType
的定义
portalSchema.statics.returnEventType = function(cb) {
cb(EVENT);
};
var Portal = mongoose.model('Portal', portalSchema);
在调用 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);
});
}
您不能在模型创建后向其添加静态方法,因此请在调用 model
:
returnEventType
的定义
portalSchema.statics.returnEventType = function(cb) {
cb(EVENT);
};
var Portal = mongoose.model('Portal', portalSchema);