hurt-tomatoH
Refine3y ago
5 replies
hurt-tomato

onSettled error when creating a child item

Hey, so we've overriden the save on click handler with our own function to create an "item", then once that "item" is created, we the create an "artwork" that belongs to that item. We capture all the details in one form.

How we've done it is like this:

const { mutate } = useCreate();
const onSave = () => {
        mutate({
            resource: "item",
            values: {
                ...itemValues,
            },
        },
            {
                onSuccess: (data) => {
                    const id = data.data.id;
                    const formData = new FormData();
                    formData.append('file', artworks[0]);

                    Object.keys(artworkValues).forEach((k, i) => {
                        var d = Object.values(artworkValues)[i];
                        formData.append(k, d);
                    })

                    mutate({
                        resource: `item/${id}/artwork`,
                        values: formData,
                    });
                },
            });
        };

<Create isLoading={formLoading} saveButtonProps={{ ...saveButtonProps, disabled: isSaveDisabled, onClick: onSave }}>


This works, it created an item, then creates an artwork for us, no problem.

However, we get the following error, which is caused by the artwork mutate, as when we comment it out the error doesn't happen.

mutation.ts:261 TypeError: Cannot read properties of undefined (reading 'onSettled')
    at mutationObserver.ts:177:1
    at Object.batch (notifyManager.ts:25:1)
    at MutationObserver.notify (mutationObserver.ts:168:1)
    at MutationObserver.onMutationUpdate (mutationObserver.ts:100:1)
    at mutation.ts:356:1
    at Array.forEach (<anonymous>)
    at mutation.ts:355:1
    at Object.batch (notifyManager.ts:25:1)
    at Mutation.dispatch (mutation.ts:354:1)
    at Mutation.execute (mutation.ts:248:1)


Is there a better more
refine
way of doing what we are doing here?
Was this page helpful?