'How to React-Admin Use JsonServer Get My express API "The response to 'getList' must be like { data : [...] }
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;
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 });
}
});
My Postman...
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'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
