问题
React-Admin version: 2.4.0
I have a list with a info of wallets that I can list using this endpoint:
http://myHost:8080/api/v1/private/wallets
Now I want to show data of the movements of each wallet when I click in the wallet of the list.
So, for obtain the data of the movements of the wallet I need to call to another endpoint, sending the get with this format:
${apiUrl}/${resource}/${params.id}/movements
For example:
http://myHost:8080/api/v1/private/wallets/cd496094-a77a-4e4e-bcd9-3361ff89294a/movements
This endpoint return me an object like this:
{
"_embedded": {
"movements": [
{
"id": "ftr4e2e5-a2bf-49f7-9206-3e2deff3a456",
"amount": 10,
"status": "PENDING"
},
{
"id": "67732ad9-233e-42be-8079-11efe4d158yt",
"amount": 2.56,
"status": "SUCCESS"
}
]
}
}
I have this code:
//IMPORTS
export const WalletList = props => (
<List {...props}>
<Datagrid rowClick="show">
<TextField source="id" label="Wallet ID" />
<TextField source="iban" label="IBAN" />
</Datagrid>
</List>
);
export const WalletShow = props => (
<Show {...props}>
<TabbedShowLayout>
<Tab label="Wallet">
<TextField label="Wallet ID" source="id" />
</Tab>
<Tab label="Movements">
<TextField label="Movement ID" source="id" />
</Tab>
</TabbedShowLayout>
</Show>
);
I have a custom data provider based in json-server for this endpoint:
export default (apiUrl, httpClient = fetchUtils.fetchJson) => {
const convertDataRequestToHTTP = (type, resource, params) => {
let url = '';
const options = {};
switch (type) {
case GET_LIST: {
//...
}
case GET_ONE:
// custom call for movements: /api/v1/private/wallets/{walletId}/movements
if (resource === 'wallets') {
url = `${apiUrl}/${resource}/${params.id}/movements`;
break;
}
url = `${apiUrl}/${resource}/${params.id}`;
break;
case GET_MANY_REFERENCE:
//...
case UPDATE:
//...
case CREATE:
//...
case DELETE:
//...
default:
throw new Error(`Unsupported fetch action type ${type}`);
}
return { url, options };
};
//...
}
And my error is:
The response to 'GET_ONE' must be like { data: { id: 123, ... } }, but the received data does not have an 'id' key. The dataProvider is probably wrong for 'GET_ONE'
How I can list inside a show component?
回答1:
First, about your dataProvider
, as explained in the [documentation(https://marmelab.com/react-admin/DataProviders.html#response-format), you must return an object with a data
key containing your API response. This will fix the error you're seeing.
Second, about having a list of related data inside a show view, you should use a ReferenceManyField inside your tab. It will requests all movements
linked to the current wallet
using the GET_MANY_REFERENCE
fetch type.
来源:https://stackoverflow.com/questions/52931110/how-can-i-list-data-from-another-endpoint-inside-show-option-in-react-admin