Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useCreateResult data contains the update parameters and not the response data #7912

Closed
dylanlt opened this issue Jun 30, 2022 · 5 comments · Fixed by #7925
Closed

useCreateResult data contains the update parameters and not the response data #7912

dylanlt opened this issue Jun 30, 2022 · 5 comments · Fixed by #7925
Labels

Comments

@dylanlt
Copy link

dylanlt commented Jun 30, 2022

What you were expecting:
useCreateResult.data will contain all fields returned from the API, just like useUpdateResult.data does.

What happened instead:
useCreateResult.data contains the data sent to the API instead of the data returned from it.

Steps to reproduce:
Any API that returns a calculated value in one of the fields (i.o.w. the field is not part of the data sent to the API, but rather generated by the API for consumption by the RA app) and a custom data provider method in RA that consumes the value, similar to the logo upload example. I ran into this when doing logo uploads using S3 signed URL.

Related code:

export const CustomDataProvider = {
	...baseDataProvider,
		updateWithUpload: async (resource: string, record: any) => {
		        const { logoUrl, logoName, ...rest } = record;
			// call our API to get signedUrl property
			const updateResult = await baseDataProvider.update(resource, {
				id: record.id,
				data: {
					...rest,
					logoName: logoUrl.rawFile.path,
				},
				previousData: { id: record.id },
			});
			if (updateResult.data.signedUrl) {
			  // this works
			}
		},
		createWithUpload: async (resource: string, record: any) => {
		        const { logoUrl, logoName, ...rest } = record;
			// call our API first to get signedUrl property
			const createResult = await baseDataProvider.create(resource, {
				data: {
					...rest,
					logoName: logoUrl.rawFile.path,
				},
			});
			// upload logo directly to s3
			if (createResult.data.signedUrl) {
			   // we never reach this code
			}
		}

Other information:
N/A

Environment

  • React-admin version: 3.19.5
  • Last version that did not exhibit the issue (if applicable):
  • React version: 16.14.0
  • Browser: Chrome latest on Mac
  • Stack trace (in case of a JS error):
@fzaninotto
Copy link
Member

We fixed a similar bug in v4 in #7783

@fzaninotto fzaninotto added the v3 label Jun 30, 2022
@fzaninotto
Copy link
Member

I don't understand how that is possible looking at the react-admin code: The create success action uses the response payload (the data returned by the API), and that's what is used in the data reducer.

The problem may lie in your dataProvider.

Can you build a reproduction case based on the simple example codesandbox?

https://codesandbox.io/s/github/marmelab/react-admin/tree/3.x/examples/simple

@dylanlt
Copy link
Author

dylanlt commented Jul 4, 2022

The problem is actually in ra-data-simple-rest. Compare these two section for create and update and we see that create returns params.data and update returns json. It would seem it's more important to rather return the API response because it will contain database defaults like created timestamp that does not exist in the request parameters. Am I missing something?

    update: (resource, params) =>
        httpClient(`${apiUrl}/${resource}/${params.id}`, {
            method: 'PUT',
            body: JSON.stringify(params.data),
        }).then(({ json }) => ({ data: json })),
        
      create: (resource, params) =>
        httpClient(`${apiUrl}/${resource}`, {
            method: 'POST',
            body: JSON.stringify(params.data),
        }).then(({ json }) => ({
            data: { ...params.data, id: json.id },
        })),

@dylanlt dylanlt closed this as completed Jul 4, 2022
@dylanlt dylanlt reopened this Jul 4, 2022
@fzaninotto
Copy link
Member

fzaninotto commented Jul 4, 2022

You're right, the problem comes from ra-data-simplerest. Would you mind opening a PR to fix it?

Note: the problem occurs both in v3 and v4.

@dylanlt
Copy link
Author

dylanlt commented Jul 4, 2022

Done!
#7925

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants