如何在Strongloop上为自定义远程方法设置自定义架构

jrl*_*ltt 12 methods model response strongloop loopbackjs

我是Strongloop的新手,我找不到有关如何自定义响应类(我构建的对象的模型架构)的信息,我不知道如何在API资源管理器上显示带有自定义数据的对象.

捕获strongloop api探险家

例如,我有一个名为score的自定义远程方法

POST /Challenges/score
Run Code Online (Sandbox Code Playgroud)

我想为参数显示data一个自定义模型模式而不是单个参数,而不是模型模式用于挑战,正文上的数据具有所有参数并向用户显示数据类型:模型模式,这可能吗?

{
  "id": "string",
  "limit": 0,
  "order": "string",
  "userId": "string"
}
Run Code Online (Sandbox Code Playgroud)

另一方面,在Response Class中,我想显示响应对象的模式.像这样的东西:

{
  "id":"string",
  "userId":"string",
  "user": {},
  "totalScore":0,
  "tags": []
}
Run Code Online (Sandbox Code Playgroud)

我看了不同的问题(这个这个),但找不到解决这个问题的东西.

更新

这是远程方法的定义

Challenge.remoteMethod('score', {
    accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
    returns: {arg: 'scores', type: 'array'},
    http: {path: '/score', verb: 'post'}
});
Run Code Online (Sandbox Code Playgroud)

小智 8

我相信你可能已经通过了windloop的官方文档.如果没有,这里是解释远程方法及其接受的数据类型的链接.https://docs.strongloop.com/display/public/LB/Remote+methods

假设您的自定义对象是Challenge,要显示响应中的对象,您必须指定类型(类型可以是loopback的数据类型之一,也可以是自定义模型).因此,要返回Challenge,您必须添加以下代码:

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

您指定的第二个箭头是您希望通过API调用尝试的默认值.您可以传递任何自定义字符串,默认为键.例如,如果要传递某个对象:

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'
    }
});
Run Code Online (Sandbox Code Playgroud)

因此,对于响应,您无法自定义模型.但要传递默认值,您可以将任何内容放入字符串格式中.


pri*_*rmx 5

在环回中,远程参数可以识别使用 ds.define('YourCustomModelName', dataFormat); 定义的数据模型。

因此,对于您的情况,请在Challenge.js文件中编写一个函数,该文件将定义一个远程方法(在您的情况下为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);
};
Run Code Online (Sandbox Code Playgroud)

在远程参数类型下使用 'type': 'YourCustomModelName'

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

重新启动服务器并刷新后,您应该会看到它反映在资源管理器上:)


jrl*_*ltt 2

我发现解决这个问题的方法是以这种方式创建一个新模型,并使用助手slc loopback: model

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

我继续添加属性,然后放在 Challenge.js 上:

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

那行得通!如果有人知道更好的方法,请分享。