简体   繁体   中英

How do I get a hold of a Strongloop loopback model?

This is maddening, how do I get a hold of a loopback model so I can programmatically work with it ? I have a Persisted model named "Notification". I can interact with it using the REST explorer. I want to be able to work with it within the server, ie Notification.find(...). I execute app.models() and can see it listed. I have done this:

var Notification = app.models.Notification;

and get a big fat "undefined". I have done this:

var Notification = loopback.Notification;
app.model(Notification);
var Notification = app.models.Notification;

and another big fat "undefined".

Please explain all I have to do to get a hold of a model I have defined using:

slc loopback:model

Thanks in advance

You can use ModelCtor.app.models.OtherModelName to access other models from you custom methods.

/** 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
  }
}

I know this post was here a long time ago. But since I got the same question recent days, here's what I figured out with the latest loopback api:

You can get the application which your model was attached as following:

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} } ); }; 

Looks like I'm not doing well with the code block format here...

Also you should be careful about the timing when this 'getApp' get called, it matters because if you call this method very early when initializing the model, something like 'undefined' error will occur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM