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

时间:2016-03-04 09:41:30

标签: methods model response loopbackjs strongloop

我是Strongloop上的新手,我无法找到有关如何自定义响应类的信息(我构建的对象的模型架构),而且我不知道如何在API上显示使用自定义数据探索对象。

Capture for strongloop api explorer

例如,我有一个名为score

的自定义远程方法
POST /Challenges/score

我想为参数data显示一个自定义模型架构而不是单个参数,而不是针对Challenge的Model Schema,正文上的数据具有所有参数并在数据类型上向用户显示: Model Schema,这可能吗?

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

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

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

我看了不同的问题(thisthis),但无法找到解决此问题的方法。

更新

以下是远程方法的定义

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

5 个答案:

答案 0 :(得分:8)

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

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

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

您指定的第二个箭头是您希望通过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'
    }
});

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

答案 1 :(得分:3)

@jrltt,而不是使用默认,使用指向接受下的类型的对象结构,它应该可以工作。注意, http source:body 是必需的。

使用随机对象:

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'
    }
});

使用模型配置中可用的已定义模型或使用环回模型生成器创建,然后该模型名称可用于指向类型。 因此,让我们使用用户模型在接受参数

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

答案 2 :(得分: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

我继续把属性放在Challenge.js上:

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

这很有效!如果有人知道更好的方法,请分享。

答案 3 :(得分:2)

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

因此,对于您的情况,请在 Challenge.js 文件中编写一个函数,该文件将定义一个远程方法(例如得分)。

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);
};

在远程参数下输入'键入':' YourCustomModelName'

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

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

答案 4 :(得分:0)

我找到了一种方法,可以通过更改accepts数组中的type参数来解决此问题。 当我们创建remoteMethod时;我们提供accepts数组。有 arg,类型,必填,http 。那么我们可以将请求对象设置为 type 参数。

示例代码

UserModel.remoteMethod(
 'login',
  {
    description: 'Login a user with username/email and password.',
    accepts: [
      {
        arg: 'credentials',
        type: {'email': 'string', 'password': 'string'},
        required: true,
        http: {source: 'body'},
      },
      {
        arg: 'include', type: ['string'], http: {source: 'query'},
        description: 'Related objects to include in the response. ' +
          'See the description of return value for more details.',
      },
    ],
    returns: {
      arg: 'accessToken', type: 'object', root: true,
      description:
        g.f('The response body contains properties of the {{AccessToken}} created on login.\n' +
          'Depending on the value of `include` parameter, the body may contain ' +
          'additional properties:\n\n' +
          '  - `user` - `U+007BUserU+007D` - Data of the currently logged in user. ' +
          '{{(`include=user`)}}\n\n'),
    },
    http: {verb: 'post'},
  },
);