如何 React-Admin 使用 JsonServer 获取我的express API“对‘getList’的响应”必须类似于 { data : [...] }

发布于 2025-01-09 04:29:35 字数 2610 浏览 1 评论 0原文

我使用 jsonServerProvider 但我无法获取我的 Express Api 数据

错误代码

The response to 'getList' must be like { data : [...] }, but the received data is not an array. The dataProvider is probably wrong for 'getList'

My React-Admin ./dataProvider...

import jsonServerProvider from "ra-data-json-server";
import {fetchUtils} from "react-admin"
import axios from 'axios'

const fetchJson = (url, options = {}) => {
  if (!options.headers) {
      options.headers = new Headers({ Accept: 'application/json' });
  }
  // add your own headers here
  options.headers.set('X-Custom-Header', 'foobar');
  options.headers.set('withCredentials', true);
  axios.defaults.withCredentials = true;
  return fetchUtils.fetchJson(url, options);
}

const dataProvider = jsonServerProvider("http://localhost:5000/periodical/api",fetchJson);

export default dataProvider; 

在此处输入图像描述

我的类别列表...

import * as React from "react";
import {
  List,
  Datagrid,
  TextField,
  EditButton,
  Create,
  Edit,
  SimpleForm,
  SelectInput,
  TextInput,
  DeleteButton,
} from "react-admin";


const CategorysTitle = ({ record }) => {
  return <span>Post {record ? `"${record.title}"` : ""}</span>;
};

export const CategorysList = (props) => (
  <List {...props}>
    <Datagrid>
      <TextField source="id" />
      <TextField source="name" />
      <EditButton />
      <DeleteButton />
    </Datagrid>
  </List>
);

我的 NodeJS Express...

router.route('/').get(async (req, res) => {
    try {
        const data = await CATEGORY.get();
        res.set('Access-Control-Expose-Headers', 'X-Total-Count')
        res.set('X-Total-Count', data)
        console.log(data);
        return res.status(200).json({ data });
    } catch (error) {
        return res.status(500).json({ error });
    }
});

在此处输入图像描述

我的邮差...

在此处输入图像描述

我的网站...

The response to 'getList' must be like { data : [...] }, but the received data is not an array. The dataProvider is probably wrong for 'getList'

在此处输入图片描述

I use jsonServerProvider But I can not get my express Api data

error code

The response to 'getList' must be like { data : [...] }, but the received data is not an array. The dataProvider is probably wrong for 'getList'

My React-Admin ./dataProvider...

import jsonServerProvider from "ra-data-json-server";
import {fetchUtils} from "react-admin"
import axios from 'axios'

const fetchJson = (url, options = {}) => {
  if (!options.headers) {
      options.headers = new Headers({ Accept: 'application/json' });
  }
  // add your own headers here
  options.headers.set('X-Custom-Header', 'foobar');
  options.headers.set('withCredentials', true);
  axios.defaults.withCredentials = true;
  return fetchUtils.fetchJson(url, options);
}

const dataProvider = jsonServerProvider("http://localhost:5000/periodical/api",fetchJson);

export default dataProvider; 

enter image description here

My categoryList...

import * as React from "react";
import {
  List,
  Datagrid,
  TextField,
  EditButton,
  Create,
  Edit,
  SimpleForm,
  SelectInput,
  TextInput,
  DeleteButton,
} from "react-admin";


const CategorysTitle = ({ record }) => {
  return <span>Post {record ? `"${record.title}"` : ""}</span>;
};

export const CategorysList = (props) => (
  <List {...props}>
    <Datagrid>
      <TextField source="id" />
      <TextField source="name" />
      <EditButton />
      <DeleteButton />
    </Datagrid>
  </List>
);

My NodeJS express...

router.route('/').get(async (req, res) => {
    try {
        const data = await CATEGORY.get();
        res.set('Access-Control-Expose-Headers', 'X-Total-Count')
        res.set('X-Total-Count', data)
        console.log(data);
        return res.status(200).json({ data });
    } catch (error) {
        return res.status(500).json({ error });
    }
});

enter image description here

My Postman...

enter image description here

My Web...

The response to 'getList' must be like { data : [...] }, but the received data is not an array. The dataProvider is probably wrong for 'getList'

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2025-01-16 04:29:35

尝试调整您的 API 以在其顶层返回一个数组,如下所示:
[
{
“id”:“C01”,
“姓名”:“姓名01”
},
{
“id”:“C02”,
“姓名”:“姓名02”
}
]

这样您就可以安全地丢弃 ...{ "data": [... 以免 API 返回。

在react-admin文档中看到的“数据”和“总计”标签指的是dataProvider的可视化工作。在 ra-data-json-server 包上查看它: https://github.com/marmelab/react-admin/blob/1b6967f3367b13a6675de7047bd9385dc36a8db5/packages/ra-data-json-server/src/index.js#L113

Try adjusting your API to return an array at its top level like so:
[
{
"id": "C01",
"name": "name01"
},
{
"id": "C02",
"name": "name02"
}
]

so that you can safely discard the ...{ "data": [... from being returned by your API.

That "data" and "total" labels seen in react-admin documentation are referring to the dataProvider's job for visualisation. See it on ra-data-json-server package: https://github.com/marmelab/react-admin/blob/1b6967f3367b13a6675de7047bd9385dc36a8db5/packages/ra-data-json-server/src/index.js#L113

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文