13

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, i.e. 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

Jordan Kasper
  • 12,144
  • 3
  • 33
  • 52
Warren
  • 713
  • 7
  • 18
  • See https://groups.google.com/forum/#!topic/loopbackjs/Z5VNL5Aw4Cs – Raymond Feng Oct 21 '14 at 18:43
  • Could be useful to someone: if you access model before it is "initialized" it gives undefined. Just try to access model from script placed in server/boot. Good example here: http://docs.strongloop.com/display/public/LB/Defining+boot+scripts#Definingbootscripts-Synchronousbootscripts – IvanZh Dec 26 '14 at 21:10

2 Answers2

10

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
  }
}
Miroslav Bajtoš
  • 9,924
  • 1
  • 36
  • 91
  • What if you don't have another model constructor in your code but you do have an instance of the model? – JBCP Dec 12 '15 at 04:29
  • 1
    @JBCP you can get hold of the constructor via `constructor` property (usually). E.g. "productInstance.constructor.app.models.Randomizer". – Miroslav Bajtoš Dec 14 '15 at 08:57
  • Thanks. If I have a model instance, is it safe to just do app.models.OtherModel, since I know the app is initialized by now? That is what I have been doing so far. – JBCP Dec 14 '15 at 12:27
  • 1
    @JBCP Most likely (99%) yes. – Miroslav Bajtoš Dec 14 '15 at 14:52
0

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.

Qiushi
  • 133
  • 1
  • 6