Warren
Warren

Reputation: 755

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, 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

Upvotes: 13

Views: 6991

Answers (2)

Qiushi
Qiushi

Reputation: 223

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.

Upvotes: 0

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10785

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

Upvotes: 10

Related Questions