2015-01-28 9 views
1

관련 솔루션은몽구스 정적 유형 오류 : 몽구스의 호출 정적 방법은 내가이 오류를 얻고있다 그리고 난 찾을 수없는 아직도 그 오류를 검색하지만, 반면 이러한 방법

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

} 

답변

2

당신은 그것이 만들어지는 후에 모델에 정적 메소드를 추가, 그래서 model 호출하기 전에 returnEventType의 정의를 이동할 수 없습니다 :

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

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

감사 많은이 : 정말 도움이되었다 –