Shailesh Shekhawat
Shailesh Shekhawat

Reputation: 235

Mongoose statics Type Error : no such method

while calling static method of mongoose i am getting this error and i have searched for that error but still can't find relevant solution to resolve it

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'

Model:

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);
};

Controller:

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);
        });

}

Upvotes: 1

Views: 747

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311975

You can't add static methods to your model after it's created, so move the definition of returnEventType before the call to model:

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

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

Upvotes: 3

Related Questions