War*_*ren 12 javascript node.js strongloop loopbackjs
这是令人抓狂的,我如何获得一个环回模型,以便我可以以编程方式使用它?我有一个名为"通知"的持久模型.我可以使用REST资源管理器与它进行交互.我希望能够在服务器中使用它,即Notification.find(...).我执行app.models()并可以看到它列出.我这样做了:
var Notification = app.models.Notification;
Run Code Online (Sandbox Code Playgroud)
得到一个大胖子"未定义".我这样做了:
var Notification = loopback.Notification;
app.model(Notification);
var Notification = app.models.Notification;
Run Code Online (Sandbox Code Playgroud)
和另一个大胖子"未定义".
请解释我所要做的所有事情,以获得我使用的模型:
slc loopback:model
Run Code Online (Sandbox Code Playgroud)
提前致谢
Mir*_*toš 10
您可以使用ModelCtor.app.models.OtherModelName自定义方法访问其他模型.
/** common/models/product.js **/
module.exports = function(Product) {
Product.createRandomName = function(cb) {
var Randomizer = Product.app.models.Randomizer;
Randomizer.createName(cb);
}
// this will not work as `Product.app` is not set yet
var Randomizer = Product.app.models.Randomizer;
}
/** common/models/randomizer.js **/
module.exports = function(Randomizer) {
Randomizer.createName = function(cb) {
process.nextTick(function() {
cb(null, 'random name');
});
};
}
/** server/model-config.js **/
{
"Product": {
"dataSource": "db"
},
"Randomizer": {
"dataSource": null
}
}
Run Code Online (Sandbox Code Playgroud)