2017-07-04 26 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 
     } 
    }) 
} 

上述代码已在窗口中格式化,因此可能无法直接使用。但我希望你明白这一点。