我想在 server.js 中使用自定义环回模型,如下所示。
服务器/server.js
app.start = function() {
// start the web server
return app.listen(function() {
app.emit('started');
console.log('Web server listening at: %s', app.get('url'));
});
};
if (require.main === module) {
var server = app.start();
app.models.Foo.something(); // <- call method here
}
常见/模型/foo.js
module.exports = function(Foo) {
Foo.prototype.something = function() {
console.log('Hi');
return true;
};
Foo.setup = function() {
Foo.base.setup.apply(this, arguments);
};
Foo.setup();
};
参考:https ://gist.github.com/bajtos/213d5dae87e19f47db5d
但实际上我遇到了这样的错误。
/Projects/repo/server/server.js:45
app.models.Foo.something();
^
TypeError: Object function ModelConstructor(data, options) {
if (!(this instanceof ModelConstructor)) {
return new ModelConstructor(data, options);
}
if (ModelClass.settings.unresolved) {
throw new Error('Model ' + ModelClass.modelName + ' is not defined.');
}
ModelBaseClass.apply(this, arguments);
} has no method 'something'
at Object.<anonymous> (/Projects/repo/server/server.js:45:22)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
该方法似乎没有定义。如何在启动服务器时使用自定义方法以及如何更正它?