2016-03-04 83 views
8

我是Strongloop的新手,我无法找到有关如何自定义我的响应类的信息(我构建的对象的模型架构),我不知道如何在API浏览器上显示具有自定义数据的对象。如何在Strongloop上为自定义远程方法设置自定义架构

Capture for strongloop api explorer

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

POST /Challenges/score 

我想说明的参数data自定义模型模式,而不是单一的参数,而不是模型架构的挑战,身体上的数据具有所有参数并在数据类型:模型模式中向用户显示,这可能吗?

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

另一方面,在响应类中,我想显示响应对象的模式。事情是这样的:

{ 
    "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'} 
}); 
+0

请告诉我们你是如何定义的后远程方法。 –

+0

@RaymondCamden我用远程方法更新了这个问题 – jrltt

+0

好吧,所以我在解析确切的问题时遇到了问题。你是说你将一组自定义的数据返回并且你想要记录这些数据吗?如果是这样,你能显示你用来生成结果的代码(分数)吗?你是否也在说你想为输入定义'数据'? –

回答

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

而且工作!如果有人知道更好的方式来做到这一点,请分享。

7

我相信你可以通过strongloop的官方文档也水涨船高的定义。如果没有,这里是解释远程方法和他们接受的数据类型的链接。 https://docs.strongloop.com/display/public/LB/Remote+methods

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

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

所以,响应你不能自定义模式。但要传递默认值,您可以将任何内容放入字符串格式中。

+0

对不起!起初我认为这是一个简单的想法,但没有时间去尝试。昨天我试了一下,直到今天我都无法回答你,当定义一个默认的时候,回送会忽略它并继续显示基本模型的结构 – jrltt

0

@jrltt,而是下使用默认,使用对象结构指向类型的接受,它应该工作。请注意,http源:正文是必要的。

随着随机对象:

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

在回送,远程参数可以识别已经被使用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' 
     } 
    }); 

你应该看到它反映资源管理器重新启动服务器和清爽:)

相关问题