How do I get a hold of a Strongloop loopback model?
Asked Answered
S

2

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

Scission answered 20/10, 2014 at 1:26 Comment(2)
See groups.google.com/forum/#!topic/loopbackjs/Z5VNL5Aw4CsPratincole
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: docs.strongloop.com/display/public/LB/…Drawknife
D
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
  }
}
Dunedin answered 24/10, 2014 at 7:50 Comment(4)
What if you don't have another model constructor in your code but you do have an instance of the model?Wane
@Wane you can get hold of the constructor via constructor property (usually). E.g. "productInstance.constructor.app.models.Randomizer".Occur
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.Wane
@Wane Most likely (99%) yes.Occur
T
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.

Tarryn answered 12/7, 2015 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.