Search code examples
react-admin

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


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.


Solution

  • 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.