猿问

将数据保存到两个不同的资源

我正在尝试保存用户的完整记录,但我依赖于两个单独的表。配置文件数据保存在一个资源中,地址保存在另一个资源中。如何编写代码,以便它首先保存配置文件,然后从生成的id保存地址?有可能?

这是我的创建用户代码:


export const BarberCreate = (props) => {

  return (

    <Create {...props}>

      <TabbedForm toolbar={<BarberCreateToolbar />}>

        <FormTab label="Perfil">

          <TextInput source="name" />

          <TextInput source="email" />

          <DateInput source="birthday" />

          <TextInput source="phone" placeholder="(99) 99999-9999" />

          <TextInput source="transport" />

        </FormTab>


        <FormTab label="Endereço">

          <TextInput source="street" label="Rua" />

          <TextInput source="city" label="Cidade" />

          <TextInput source="district" label="Bairro" />

        </FormTab>

      </TabbedForm>

    </Create>

  );

};```


暮色呼如
浏览 113回答 1
1回答

largeQ

最好的方法是在后端执行此操作,可能具有事务支持,但是如果您不能,那么在react-admin方面进行的一种方法是装饰dataProvider。v3const userSaverDataProvider = dataProvider => ({&nbsp; &nbsp; ...dataProvider,&nbsp; &nbsp; create: async (resource, params) => {&nbsp; &nbsp; &nbsp; &nbsp; if (resource === 'users') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const profile = await dataProvider.create('profile', {data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: params.data.name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await dataProvider.create('address', {data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; profileID: profile.data.id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; street: params.data.street,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return profile&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return dataProvider.create(resource, params)&nbsp; &nbsp; }})也许有必要装饰创建许多,如果你在react-admin上批量创建用户。另外,结账 https://github.com/FusionWorks/react-admin-google-maps,它很有用
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答