Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The response to 'GET_LIST' must be like { data : [...] }, but the received data is not an array

Tags:

react-admin

I am trying to use the ra-data-simple-rest data provider.

import React from 'react';
import { Admin, fetchUtils, Resource } from 'react-admin';
import simpleRestProvider from 'ra-data-simple-rest';
import { PostList } from './Posts';

const dataProvider = simpleRestProvider('http://localhost:8000');

const App = () => (
    <Admin dataProvider={dataProvider}>
        <Resource name="posts" list={PostList} />
    </Admin>
);

export default App;

The react-admin listens to http://localhost:3000/#/posts

Api is running on : http://localhost:8000/posts which returns response as.

$response = new JsonResponse($output);
$response->headers->set('Content-Range', 'posts 0-0/5');
$response->headers->set('Access-Control-Expose-Headers', 'Content-Range');
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");

which retuns something as

{
    "data": [
        {
            "id": "1",
            "title": "Hello world",
            "body": "Hello world"
        },
        {
            "id": "2",
            "title": "Hello world",
            "body": "Hello world"
        }
    ],
    "total": 2
}

I am getting an error as

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

If the data key is removed as

[
    {
        "id": "1",
        "title": "Hello world",
        "body": "Hello world"
    },
    {
        "id": "2",
        "title": "Hello world",
        "body": "Hello world"
    }
]

is getting the result.

like image 286
Hari K T Avatar asked Jul 31 '19 18:07

Hari K T


1 Answers

Finally I understood this is working as expected by react-admin.

The problem was in the documentation I noticed

enter image description here

which confused me.

Looking more into the https://github.com/marmelab/react-admin/blob/1b6967f3367b13a6675de7047bd9385dc36a8db5/packages/ra-data-simple-rest/src/index.js#L118 I understood the data is rebuild by the provider.

like image 75
Hari K T Avatar answered Oct 23 '22 03:10

Hari K T