我如何获得一个Strongloop回送模型?
这是令人生气的,我如何获得一个回环模型,以便我可以以编程方式使用它? 我有一个名为“通知”的Persisted模型。 我可以使用REST资源pipe理器与它进行交互。 我想能够在服务器中使用它,即Notification.find(…)。 我执行app.models()并可以看到它列出。 我已经这样做了:
var Notification = app.models.Notification;
并得到一个大的“未定义的”脂肪。 我已经这样做了:
var Notification = loopback.Notification; app.model(Notification); var Notification = app.models.Notification;
还有另外一个胖子“未定义”。
请解释我所要做的一切,以获得我已经定义的模型:
slc loopback:model
提前致谢
您可以使用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 } }
我知道这个post在很久以前就在这里。 但是由于最近几天我得到了同样的问题,下面是我最近的回送api的想法:
- Loopback 2.19.0(7月12日最新)
- API,获取模型附加到的应用程序对象: http : //apidocs.strongloop.com/loopback/#model-getapp
你可以得到你的模型附加的应用程序如下:
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} } ); };