I just want to expand on documentation of remote methods and hope I can save you some time.
The documentation shows how to return a string and we can easily see how it would work for any primitive data types.
The handler (i.e. Person.great function) returns a string, the REST API, wraps it with a JSON object and returns
{ "greetings": "Greetings... John!" }
.
Now let say we have a method returning the address where this person lives. The handler would return an object (e.g. a model).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module.exports = function(Person) {
Person.address = function(personId, cb) {
// address is an object that would look like
// { street: '22 Main St', city: 'San Jose', zipcode: 95148 }
var address = ...; // do something to lookup the address based on personId
cb(null, address);
}
Person.remoteMethod('address', {
http: { path: '/:personId/address', verb: 'get' },
accepts: { arg: 'personId', type: 'number', required: true },
returns: { type: 'object', root: true }
});
};
The thing I want to point out here is the property root: true
on line 13.
Without it, this REST endpoint would return a JSON like this
{
"undefined": {
"street": "22 Main St",
"city": "San Jose",
"zipcode": "95148"
}
}
I found out about this by enabling debugging by starting the app this way:
$ DEBUG=loopback:explorer:routeHelpers node .
You can see how the default API endpoints are defined. They are very useful examples to follow.