1

我是 REST 管理员的新手。我的回应/users是这样的:

{
  "status": 200,
  "response": {
    "data": [
      {
        "id": 298487355,
        "login": "000000000053"
      },
      ...
    ]
  }
  "error": "text error"
}

如何为 :: 设置路径以response获取用户列表?谢谢data[...]

4

2 回答 2

2

你可以自定义你的restClient。例如,我选择与之合作,jsonServer所以我有这个app.js

import customRestClient from './customRestClient'
const App = () => (
    <Admin restClient={customRestClient('http://path.to.my.api/')}>

customRestClient实际上就是这个文件,我把它带到我的源,调整导入。

此文件是您的数据进入和从您的应用程序到您的 api 的点。

所以在convertHTTPResponseToREST函数中你可以简单地检查resource,如果它是users你可以访问你的数据 json.response.data并将其返回switch

于 2017-05-09T08:27:50.747 回答
0

非常感谢@pelak。我只是根据您的回答编写代码。

在您的自定义restClient 中指出响应数据的路径。

const convertHTTPResponseToREST = (response, type, resource, params) => {
    const { headers, json } = response;
    switch (type) {
        case GET_LIST:
        case GET_MANY_REFERENCE:
            if (!headers.has('content-range')) {
                throw new Error(
                    'The Content-Range header is missing in the HTTP Response. The simple REST client expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?'
                );
            }
            // IF you just want use with **users** resource.
            if(resource === 'users') {
                return {
                    data: json.result,
                    total: parseInt(
                        headers
                            .get('content-range')
                            .split('/')
                            .pop(),
                        10
                    ),
                };
            }
            return {
                data: json.result,
                total: parseInt(
                    headers
                        .get('content-range')
                        .split('/')
                        .pop(),
                    10
                ),
            };
        case CREATE:
            return { data: { ...params.data, id: json.id } };
        default:
            return { data: json };
    }
};

关键字:子元素中的列表,列表嵌套

于 2018-01-06T18:21:24.150 回答