2016-03-04 51 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' 
     } 
    }); 

你應該看到它反映資源管理器重新啓動服務器和清爽:)

相關問題