I'm trying to use multiple dataproviders in a react-admin project but I have an error:
Warning: Missing translation for key: "dataProvider is not a function"
function.console.(anonymous function) @ index.js:1452
I have my App.js
like this:
import React from 'react';
import { render } from 'react-dom';
import { Admin, Resource } from 'react-admin';
import dataProviders from './service/dataproviders';
import UserList1, from './users1';
import UserList2, from './users2';
const App = () => (
render(
<Admin dataProvider={dataProviders}>
<Resource name="users1" list={UserList1} />
<Resource name="users2" list={UserList2} />
</Admin>,
document.getElementById('root'),
)
);
export default App;
And I have the dataproviders.js
file:
import simpleRestProvider from 'ra-data-simple-rest';
const dataProviders = [
{ dataProvider: simpleRestProvider('http://path.to.foo.api1'), resources: ['users1'] },
{ dataProvider: simpleRestProvider('http://path.to.foo.api2'), resources: ['users2'] },
];
export default (type, resource, params) => {
const dataProvider = dataProviders.find(dp => dp.resources.includes(resource));
return dataProvider(type, resource, params);
};
How it's the correct way?
I check this post:
Is it possible to have multiple dataProviders in react-admin?
You're not calling the dataProvider
you found, you're calling the mapping object from your array. You can fix it like this:
import simpleRestProvider from 'ra-data-simple-rest';
const dataProviders = [
{ dataProvider: simpleRestProvider('http://path.to.foo.api1'), resources: ['users1'] },
{ dataProvider: simpleRestProvider('http://path.to.foo.api2'), resources: ['users2'] },
];
export default (type, resource, params) => {
const dataProviderMapping = dataProviders.find(dp => dp.resources.includes(resource));
return dataProviderMapping.dataProvider(type, resource, params);
};
来源:https://stackoverflow.com/questions/52889909/how-is-the-correct-way-to-have-multiple-dataproviders-in-react-admin