How to set up custom schema for custom remote methods on Strongloop

I am new to Strongloop, and I canโ€™t find information on how to set up a response class (model diagram for the object I built), and I donโ€™t know how to show an object with user data in the API Explorer.

Capture for strongloop api explorer

For example, I have a custom remote method called score

POST /Challenges/score 

I want to show the user model diagram instead of the individual parameters for the data parameter, and not the model diagram for calling, the data on the body have all the parameters and shows the user the data type: model diagram, is this possible?

 { "id": "string", "limit": 0, "order": "string", "userId": "string" } 

On the other hand, in the Response class, I want to show the schema for the response object. Something like that:

 { "id":"string", "userId":"string", "user": {}, "totalScore":0, "tags": [] } 

I looked at different questions ( this and this ), but I can not find something to solve these problems.

Update

Here is the definition of the remote method

 Challenge.remoteMethod('score', { accepts: { arg: 'data', type: 'object', http: { source: 'body' } }, returns: {arg: 'scores', type: 'array'}, http: {path: '/score', verb: 'post'} }); 
+8
methods response model loopbackjs strongloop
source share
4 answers

The way I found a solution to this problem is to create a new model this way using the slc loopback: model

 ? Enter the model name: ArgChallenge ? Select the data-source to attach ArgChallenge to: (no data-source) ? Select model base class PersistedModel ? Expose ArgChallenge via the REST API? No ? Common model or server only? server 

And I keep putting properties, and then on Challenge.js:

 Challenge.remoteMethod('score', { accepts: { arg: 'data', type: 'ArgChallenge', http: { source: 'body' } }, returns: {arg: 'scores', type: 'array'}, http: {path: '/score', verb: 'post'} }); 

And it works! If someone knows the best way to do this, please share.

+2
source share

I believe that you may have passed strongloop's official docs. If not, here is a link that explains the remote methods and their accepted data types. https://docs.strongloop.com/display/public/LB/Remote+methods

Assuming your custom object is a call to show the object in response, you must specify a type (the type may be one of the loopback data types or your user model). To return the Challenge , you need to add the following code:

 Challenge.remoteMethod('score', { accepts: { arg: 'data', type: 'object', http: { source: 'body' } }, returns: {arg: 'scores', type: 'Challenge', root: true}, http: {path: '/score', verb: 'post'}, }); 

The second arrow that you specified is the default values โ€‹โ€‹that you want to try with an API call. You can pass any custom string with the default as a key. For example, if you want to pass an object:

 Challenge.remoteMethod('score', { accepts: { arg: 'data', type: 'object', default: '{ "id": "string", "userId": "string", "user": {}, "totalScore": 0, "tags": [] }', http: { source: 'body' } }, returns: { arg: 'scores', type: 'Challenge' }, http: { path: '/score', verb: 'post' } }); 

So, for the answer you cannot configure the model. But to pass default values โ€‹โ€‹you can put something in string format.

+7
source share

@jrltt. Instead of using the default, use an object structure indicating the type under accepts, and it should work. Note. HTTP source: body .

With a random object:

 Challenge.remoteMethod('score', { accepts: { arg: 'data', type: { "id": "string", "userId": "string", "user": {}, "totalScore": 0, "tags": [] }, http: { source: 'body' } }, returns: { arg: 'scores', type: 'Challenge' }, http: { path: '/score', verb: 'post' } }); 

With a specific model available in the config model or creating a loopback model using the generator, then this model name can be used for the point type. Therefore, let's use the User model to display in the accepts parameter,

 Challenge.remoteMethod('score', { accepts: { arg: 'data', type: 'User', http: { source: 'body' } }, returns: { arg: 'scores', type: 'Challenge' }, http: { path: '/score', verb: 'post' } }); 
0
source share

In loopback, remote arguments can identify data models that were defined using ds.define ('YourCustomModelName', dataFormat);

so for you, write the function in the Challenge.js file, which will have a remote method (in case ur score ).

 const loopback = require('loopback'); const ds = loopback.createDataSource('memory'); module.exports = function(Challenge) { defineChallengeArgFormat() ; // remote methods (score) defined }; let defineChallengeArgFormat = function() { let dataFormat = { "id": String, "userId": String, "user": {}, "totalScore": Number, "tags": [] }; ds.define('YourCustomModelName', dataFormat); }; 

In the list of deleted arguments use 'type': 'YourCustomModelName'

  Challenge.remoteMethod('score', { accepts: { arg: 'data', type: 'YourCustomModelName', http: { source: 'body' } }, returns: { arg: 'scores', type: 'Challenge' }, http: { path: '/score', verb: 'post' } }); 

You should see that this affects the explorer after restarting the server and updating :)

0
source share

All Articles