2017-07-04 33 views
0

編輯AoR的製作者:您的框架遭受可怕的文檔。你應該真的專注於此,人們會真的採用它。如何使用couchDB設置admin-on-rest?

我不能爲我的生活解密如何管理休息做'休息'的一部分。如果有更好的文檔更好的框架,我願意接受。

我很新的反應,所以這可能是其中的一部分。

我可以辨別是

1)[管理員]標籤需要道具「RESTClient實現」,這是設置爲你的JSON源的基本路徑的功能,然後返回一個特定的功能簽名(需要3個參數,返回一個承諾)。

2)然後,[資源]標籤增加了與NAME =「上崗」的路徑,使一個列表,其中(繼承人它變成魔法)基本上沒有一個wget來你的數據庫,然後對結果進行迭代。

我想要做的事:將couchDB連接到admin-on-rest。我已經在本地主機上製作了一些測試文檔。 CouchDB的URL看起來像:

http://127.0.0.1:5984/myproject/_design/getclients/_view/getclient/

和這部作品在郵遞員,給我一個JSON對象是這樣的:

{ 
"total_rows": 4, 
"offset": 0, 
"rows": [ 
    { 
     "id": "afc3bb9218d1a5c1e81ab3cc9f004467", 
     "key": { 
      "status": "active", 
      "rating": 9.1, 
      "bio": { 
       "fname": "Sam", 
       "mname": "TestMName", 
       "lname": "TestLName", 
       "address": "712347 B Street", 
       "state": "CA", 
       "city": "Los Santos", 
       "zip": "90211", 
       "phone": "123-456-7890", 
       "email": "[email protected]", 
       "username": "TestSam", 
       "password": "abc123" 
      } 
     }, 
     "value": null 
    }, 

在這一點上我好迷茫,我不知道去哪裏找。

繼承人我現在代碼:

//App.js 

import React from 'react'; 
import { jsonServerRestClient, Admin, Resource } from 'admin-on-rest'; 

import { PostList } from './Posts.js'; 

const App =() => (

    <Admin restClient={jsonServerRestClient('http://127.0.0.1:5984/myproject/')}> 
     <Resource name="_design/getclients/_view/getclient" list={PostList} /> 
    </Admin> 
); 

export default App; 

而且

//Posts.js 
export const PostList = (props) => (
    <List {...props}> 
     <Datagrid> 
      <TextField source="status" /> 
      <TextField source="rating" /> 
     </Datagrid> 
    </List> 
); 

該頁面加載,但粉紅色的小盒子底部彈出說:

The X-Total-Count header is missing in the HTTP Response. The jsonServer REST client expects responses 

回答

1

的RESTClient實現是一個黑暗的野獸。沒有完全記錄在案。

但是,如果你知道整個事情是如何一起工作的話,它最終是非常簡單的。

1)管理員-ON-休息定義一些REST類型(下文)。這些通常是由Redux動作(在他們的meta標籤中)拍攝的。該系統掃描這些其他類型的,如果它看到他們,那麼它調用RESTClient實現

GET_LIST
GET_ONE CREATE
UPDATE
DELETE
GET_MANY GET_MANY_REFERENCE

的REST客戶端調用這些類型和其他一些參數。其餘客戶端的工作是解釋類型,然後使用參數向API發出請求。爲此,AOR使用內置於瀏覽器中的新Fetch API。

你可以通過調用訪問它。你也應該進入AOR的源代碼,並檢查了它是如何工作的。

import { fetchUtils } from 'admin-on-rest'; 

2)X總計數是AOR所有對GET_LIST類型的響應所需的標題字段。 你可以在你的API中簡單地設置它。我使用loopback,並在遠程鉤子中手動設置X-Total-Count(如果您不知道它,請不要擔心) 看來您的api仍在使用JSON服務器。 JSON服務器是一個虛擬API。所以你的應用程序現在沒有連接到你的couchDB。

https://github.com/typicode/json-server

如果你沒有使用像明示或回送一個API服務器,那麼你也可以配置你的RESTClient實現做所有的請求和響應處理。你必須構建URL。閱讀下面的鏈接,以便進一步瞭解我的示例代碼。

https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload

所以這樣的事情。

if (type === 'GET_LIST' && resource === 'posts') {   
const url = http://127.0.0.1:5984/myproject/_design/getclients/_view/getclient/ 
     options.method = 'GET'; 
     return fetchUtils.fetchJson(url, options) 
      .then((response) => { 
      const {headers, json} = response; 
      //admin on rest needs the {data} key 
      return {data: json, 
        total: parseInt(headers.get('x-total-count').split('/').pop(), 10)} 

    }) 

您也可以編寫一個像這樣的函數來處理請求和響應。

function handleRequestAndResponse(url, options={}) { 
    return fetchUtils.fetchJson(url, options) 
    .then((response) => { 
     const {headers, json} = response; 
     //admin on rest needs the {data} key 
     const data = {data: json} 
     if (headers.get('x-total-count')) { 
     data.total = parseInt(headers.get('x-total-count').split('/').pop(), 10) 
     } else { 
     data.total = json.length // this is why the X-Total-Count is needed by Aor 

} 
} 
     } 
     // handle get_list responses 
     return {data: json, 
       total: } else { 
     return data 
     } 
    }) 
} 

上述代碼已在窗口中格式化,因此可能無法直接使用。但我希望你明白這一點。