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

Response

{
    "data": [
        {
            "id": 6,
            "product_title": "Foam chair",
            "product_description": "Fantastic Foam chair",
        },
        {
            "id": 5,
            "product_title": "Shinez cleaning liquid",
            "product_description": "Shinez cleaning liquid shoes is good for electronics",
        },
        {
            "id": 4,
            "product_title": "Shinez cleaning liquid buy 1 get 1 free",
            "product_description": "Shinez cleaning liquid shoes is a good for electronics and computers",
        },
        {
            "id": 3,
            "product_title": "Raj kiran Motor cycle",
            "product_description": "Raj kiran Motor cycle old one",
        },
        {
            "id": 2,
            "product_title": "Ka ka cycle broken cycle",
            "product_description": "Ka ka cycle broken into multiple pieces",
        },
        {
            "id": 1,
            "product_title": "ka ka police cap",
            "product_description": "This is police cap used by ka team",
        }
    ]
}

But still I get the

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

here is the actual code

<BrowserRouter>
    <Admin  catchAll={NotFound} 
            dataProvider={dataProvider} 
            authProvider={authProvider} 
            loginPage={myLoginPage} >
   </Admin>
</BrowserRouter>

I am using django rest framework as backend and using 'ra-data-django-rest-framework'

Need help on this. Thanks a lot.

Had a look at the following questions React-Admin "Get List" DataProvider

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

const fetchJson = (url, options = {}) => {
  if (!options.headers) {
      options.headers = new Headers({ Accept: 'application/json' });
  }
  // Not working even if add content-range
  // Content-Range: posts 0-24/319, 
  

  //adding console.log , returns undefined here
  console.log( fetchUtils.fetchJson );
  return fetchUtils.fetchJson(url, options);
}
 const dataProvider = drfProvider('http://127.0.0.1:8000/api', fetchJson);

Imp. info: I am receiving the response on request.

adding console.log , returns undefined.

indianwebdevil
  • 4,879
  • 7
  • 37
  • 51
  • 1
    some ideas: is `fetchJson` returning a HTTP response object or json data? what does the data look like both when its straight from your API and after it has gone through your dataProvider's getList function? can you post the code for your dataprovider? – MoralCode Jun 12 '22 at 00:42
  • @MoralCode returns undefined, pls re-read code for details. – indianwebdevil Jun 12 '22 at 04:26
  • How are you importing `fetchUtils`? if its undefined at your console.log, then it cant find the function that you are trying to run – MoralCode Jun 12 '22 at 12:13

1 Answers1

0

The solution , it expects the response in this format

{
    "count": 10,
    "next": "http://127.0.0.1:8000/api/products/?page=2",
    "previous": null,
    "results": [
        {
            "id": 10,
            "product_title": "Crompton pedastal fan",
            "product_description": "Nice Crompton pedastal fan",
        },
        {
            "id": 10,
            "product_title": "Crompton pedastal fan",
            "product_description": "Nice Crompton pedastal fan",
        }
    ]
}

Digging deep opens up that, it works seamlessly with class-based views like generics view, I was using function-based view in Django. Now I changed to class based view, everything is working cool.

class ProductList (generics.ListCreateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
indianwebdevil
  • 4,879
  • 7
  • 37
  • 51