reactjsreact-admindataprovider

Error: Unknown dataProvider function: toJSON


import  restProvider   from 'ra-data-simple-rest'
const dataProvider= process.env.REACT_APP_API+'/api/link'

<Admin dataProvider={restProvider(dataProvider) }>
    <Resource name='endpoint' options={{label:'MyLabel'}} list={myList} />
  </Admin>

Response from API IS

 [{id:'',name:''},{id:'',name:''},{id:'',name:''},{id:'',name:''}]
Error: Unknown dataProvider function: toJSON
    at Proxy.<anonymous> (useDataProvider.ts:96:1)
    at JSON.stringify (<anonymous>)
    at Object.parseData `your text`(<anonymous>:8:116)
    at <anonymous>:10:2186
    at Array.reduce (<anonymous>)
    at <anonymous>:10:2114
    at i (<anonymous>:10:2706)
    at <anonymous>:10:2993
    at _ (<anonymous>:10:3119)
    at <anonymous>:10:3363

Solution

  • Need to include the option param to configure json data

    import { fetchUtils } from 'react-admin';
    
    
    
    const fetchJson = (url, options = {}) => {
        if (!options.headers) {
            options.headers = new Headers({ Accept: 'application/json' });
        } 
        return fetchUtils.fetchJson(url, options);
    }
    
    <Admin dataProvider={restProvider(dataProvider, fetchJson) }>
    

    Doc