Mongoose静态类型错误:没有这样的方法

时间:2015-01-28 07:12:07

标签: node.js mongodb mongoose discriminator

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

}

1 个答案:

答案 0 :(得分:2)

在模型创建后,您无法向模型中添加静态方法,因此在调用returnEventType之前移动model的定义:

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

var Portal = mongoose.model('Portal', portalSchema);