【问题标题】:React-Admin "Get List" DataProviderReact-Admin“获取列表”数据提供者
【发布时间】:2020-02-08 18:17:41
【问题描述】:

我开始使用react-admin 包。
我的开发受阻,因为我想用另一个Resource 的数据创建一个Select。这就是我使用ReferenceField 的原因,但我不知道为什么在使用此元素时会出现此错误。

Error: The response to 'GET_LIST' must be like { data : [{ id: 123, ...}, ...] }, but at least one received data item do not have an 'id' key. The dataProvider is probably wrong for 'GET_LIST

这是我从 API 收到的数据:

[{"_id":"5e3ec3baa6480d002b24ea90","name_promo":"test","years":"2019-01-01T00:00:00.000Z","__v":0}]

有关信息,我使用提供者ra-data-json-server

这是我的代码:

import React from 'react';
import {
    Create,
    SimpleForm,
    TextInput,
    ReferenceInput,
    SelectInput,
} from 'react-admin';

const CreateUser = (props) => (
    <Create {...props}>
        <SimpleForm>
            <TextInput source="lastName" label="Prénom" />
            <TextInput source="firstName" label="Nom" />
            <TextInput source="email" label="Email" />
            <TextInput source="role" label="Role" />
            <ReferenceInput  label="Session" source="id" reference="sessions"  >
                <SelectInput optionText="name_promo"/>
            </ReferenceInput >
        </SimpleForm>
    </Create>
);

export default CreateUser;

【问题讨论】:

  • 你能告诉我们你的DataProvider是什么样子的吗?

标签: reactjs react-admin


【解决方案1】:

您的数据从 API 返回,其 id 字段名为 "_id",而不是必需的 "id"

我们通过将以下内容添加到我们的 (python) API 中解决了这个问题。

myData = mongo.get(...)
for x in myData:
    x['id'] = x['_id']  // Now each record has both '_id' and 'id'
return {'data': myData}, HTTPStatus.CREATED

【讨论】:

    【解决方案2】:

    ra-admin 默认使用密钥 idname,如果您的 API 调用未返回该公司,您必须将其声明为组件上的 prop p>

    <ReferenceInput label="Session" source="id" reference="sessions" >
      <SelectInput optionText="name_promo" optionValue="_id" />
    </ReferenceInput>
    

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 2021-09-12
      • 2020-05-08
      • 1970-01-01
      • 2021-10-26
      • 2020-08-07
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多