【发布时间】:2014-07-15 15:10:16
【问题描述】:
我在 Node.js 应用程序中使用 Mongoose,我想使用模型继承。我正在按照此处给出的说明进行操作 Inheritance in mongoose 和其他链接,但我不知道如何继承静态方法。
这是我的尝试:
// 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);
pb 是每次我尝试使用 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.
【问题讨论】:
标签: node.js mongodb inheritance mongoose