12

I've been working on this for hours and I'm completely lost, because the loopback documentation is not helpful.

I'm trying to write application logic into a model. The documentation for that is here. Unfortunately, the example doesn't demonstrate anything useful other than passing an external value into the remote method and returning it again. I'd like to understand how to run a query in this context and access model data, but I have searched for hours and not been able to find documentation on even these simple tasks. Maybe I'm just looking in the wrong places. Can anyone help?

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Michael.Lumley
  • 2,345
  • 2
  • 31
  • 53

2 Answers2

9

Typically, you can accomplish most things you'd want to do such as querying and accessing model data (CRUD operations) through the built-in methods that all models get; see http://docs.strongloop.com/display/LB/Working+with+data. Defining a remote method (custom REST endpoint) for these would be redundant.

You access the standard model CRUD Node APIs (e.g. myModel.create(), myModel.find(), myModel.updateAll() ) in the remote method code if you want to.

You may also find further related examples in https://github.com/strongloop/loopback-example-app-logic

Here's an example using the Getting Started app https://github.com/strongloop/loopback-getting-started app. It defines a remote method that takes a number arg and prints the name of the coffeeshop with that ID to the console:

This code is in common/models/coffeeshop.js:

module.exports = function(CoffeeShop) {
...
  // Return Coffee Shop name given an ID.
  
  CoffeeShop.getName = function(shopId, cb) {
    CoffeeShop.findById( shopId, function (err, instance) {
        response = "Name of coffee shop is " + instance.name;
        cb(null, response);
        console.log(response);
    });
  }
...
  CoffeeShop.remoteMethod (
    'getName', 
    {
      http: {path: '/getname', verb: 'get'},
      accepts: {arg: 'id', type: 'number', http: { source: 'query' } },
      returns: {arg: 'name', type: 'string'}
     }
  );
};

You can use the API Explorer to load http://0.0.0.0:3000/explorer/#!/CoffeeShops/getName then enter a number (there are only three coffee shops in the app initially) as the query param and hit "Try It Out!"

Or just GET a URL like http://0.0.0.0:3000/api/CoffeeShops/getid?id=1

Rand

RandM
  • 231
  • 1
  • 3
  • Thanks for the answer, maybe I am going about this the wrong way, but I think my question is even more basic than you thought. Where are the model's properties stored? If I wanted to `console.log` the "Name" attribute of a "Person" model, from inside a remote method defined in `common/models/Person.js`, how would I do that? I can't find anything in the documentation that explains this. – Michael.Lumley Jan 27 '15 at 21:23
  • I tried to answer in a comment, but my remarks were too long. See edited answer above. In brief, you can access properties of the model within the callback function... Hope that helps! – RandM Jan 28 '15 at 00:37
  • it's worth noting that in loopback it is only the `PersistedModel` that comes with the built in methods...if you want to create a model that has a REST datasource then you do need to create custom remote methods for handling requests – br3w5 May 21 '15 at 12:04
2

I finally discovered my problem. Object properties must be loaded in a callback of the function calling the CRUD operation. The following syntax worked for me:

module.exports = function (TestModel) {
    TestModel.testRemoteMethod = function (id, name, cb) {
        TestModel.findOne({where: {id: id}}, function(err, modelInstance) {
            //modelInstance has properties here and can be returned to
            //the API call using the callback, for example:
            cb(null, {"name": modelInstance.name});
        }
    }
    TestModel.remoteMethod('testRemoteMethod',
        //..rest of config
Michael.Lumley
  • 2,345
  • 2
  • 31
  • 53