1

在调用 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);
        });

}
4

1 回答 1

3

创建模型后,您无法将静态方法添加到模型中,因此将returnEventType调用之前的定义移至model

portalSchema.statics.returnEventType = function(cb) {
    cb(EVENT);
};

var Portal = mongoose.model('Portal', portalSchema);
于 2015-01-28T14:23:51.727 回答