Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ra-data-simple-rest is not working | ERROR: total is not a number and newRecords.forEach is not a function

I am trying to follow the tutorial and I am now at the part where I have to implement my own REST-API. My format is identical to the needed format described in the tutorial. Here my response: The response from my API

The needed format looks like this:

// {
//     data: [
//         { id: 126, title: "allo?", author_id: 12 },
//         { id: 127, title: "bien le bonjour", author_id: 12 },
//         { id: 124, title: "good day sunshine", author_id: 12 },
//         { id: 123, title: "hello, world", author_id: 12 },
//         { id: 125, title: "howdy partner", author_id: 12 },
//     ],
//     total: 27
// }

But for some reason I always get the error that (1) total is not a number, but postman says it is. And (2) that newRecords.forEach is not a function.

´´´

//App.js
import logo from './logo.svg';
import React from 'react';
import { Admin, Resource, ListGuesser } from 'react-admin';
import simpleRestProvider from 'ra-data-simple-rest';
import styles from './App.css';
import SimpleFlow from './diagramm';
import { RuleList, PostList} from './rules';

//http://jsonplaceholder.typicode.com'
const dataProvider = simpleRestProvider('http://localhost:3000/api');
const App = () =>
    <Admin dataProvider={dataProvider} dashboard={SimpleFlow}>
        <Resource name="rules" list={RuleList} />
    </Admin>;

export default App;

´´´

´´´

//rules.js
import React from 'react';
import { List, Datagrid, TextField, EmailField, DateField, Pagination } from 'react-admin'; 

export const RuleList = (props) => (
    <List {...props} perPage={25}>
        <Datagrid rowClick="edit">
            <TextField source="id" />
            <TextField source="Bezeichnung" />
        </Datagrid>
    </List>

);

´´´

like image 213
Colin Degen Avatar asked Dec 06 '25 20:12

Colin Degen


1 Answers

The problem is due to your data sample format, in that case your API respose it is an object, but it need to be an array instead. Like this:

[
     { id: 126, title: "allo?", author_id: 12 },
     { id: 127, title: "bien le bonjour", author_id: 12 },
     { id: 124, title: "good day sunshine", author_id: 12 },
     { id: 123, title: "hello, world", author_id: 12 },
     { id: 125, title: "howdy partner", author_id: 12 }
]
like image 176
guilhermecpa Avatar answered Dec 08 '25 08:12

guilhermecpa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!