我在一个Node.js应用程序中使用Mongoose,并希望使用模型的继承。我正在遵循这里Mongoose中的继承和其他链接中给出的说明,但我无法弄清如何继承静态方法。
以下是我尝试的内容:
问题在于每次我尝试使用removeAll函数。例如:
我收到了以下错误信息:
感谢你提前的帮助! JM。
以下是我尝试的内容:
// The BaseSchema class :
function BaseSchema(objectName, schema) {
log.trace('BaseSchema CTOR : objectName=%s schema=%s', objectName, schema);
Schema.apply(this, [schema]);
var _objectName = objectName;
...
}
BaseSchema.prototype = new Schema();
BaseSchema.prototype.constructor = BaseSchema;
// !!! Here I try to expose the removeAll statics methods for all sub-schema !!!
BaseSchema.prototype.removeAll = function() { ... }
这里是继承类
// The inherited class
var AccountSchema = new BaseSchema('account', {
...
}
mongoose.model('Account', AccountSchema);
问题在于每次我尝试使用removeAll函数。例如:
var Account = mongoose.model('Account');
Account.removeAll(function () {
done();
});
我收到了以下错误信息:
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 'removeAll'
我尝试了不同的组合来声明removeAll方法,但都没有成功:
BaseSchema.prototype.statics.removeAll = function() { ... }
BaseSchema.statics.removeAll = function() { ... }
感谢你提前的帮助! JM。
Sample
model once compiled" 的错误提示,我是不是漏掉了什么? - Aviram Netanel