这太让人抓狂了,我怎么才能得到一个回送模型,这样我就可以通过编程来使用它了?我有一个名为"Notification“的持久化模型。我可以使用REST资源管理器与其交互。我希望能够在服务器中使用它,即Notification.find(...)。我执行了app.models(),可以看到它被列出了。我已经这样做了:
var Notification = app.models.Notification;然后得到一个巨大的“未定义的”。我已经这样做了:
var Notification = loopback.Notification;
app.model(Notification);
var Notification = app.models.Notification;和另一个大胖子“未定义”。
请解释我需要做的所有工作才能获得我定义的模型:
slc loopback:model提前感谢
发布于 2014-10-24 15:50:48
您可以使用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
}
}发布于 2015-07-12 16:07:34
我知道这篇文章很久以前就在这里了。但由于最近几天我收到了同样的问题,下面是我用最新的loopback api得出的结论:
您可以通过如下方式获取您的模型所附加的应用程序:
ModelX.js
module.exports = function(ModelX) {
//Example of disable the parent 'find' REST api, and creat a remote method called 'findA'
var isStatic = true;
ModelX.disableRemoteMethod('find', isStatic);
ModelX.findA = function (filter, cb) {
//Get the Application object which the model attached to, and we do what ever we want
ModelX.getApp(function(err, app){
if(err) throw err;
//App object returned in the callback
app.models.OtherModel.OtherMethod({}, function(){
if(err) throw err;
//Do whatever you what with the OtherModel.OtherMethod
//This give you the ability to access OtherModel within ModelX.
//...
});
});
}
//Expose the remote method with settings.
ModelX.remoteMethod(
'findA',
{
description: ["Remote method instaed of parent method from the PersistedModel",
"Can help you to impliment your own business logic"],
http:{path: '/finda', verb: 'get'},
accepts: {arg:'filter',
type:'object',
description: 'Filter defining fields, where, include, order, offset, and limit',
http:{source:'query'}},
returns: {type:'array', root:true}
}
);
};
看起来我在这里的代码块格式做得不太好…
此外,你应该注意这个'getApp‘被调用的时间,这很重要,因为如果你在初始化模型的时候很早就调用了这个方法,就会发生类似'undefined’的错误。
https://stackoverflow.com/questions/26457327
复制相似问题