marmelab/react-admin · error · Error

useCreate mutation requires a non-empty data object

Error message

useCreate mutation requires a non-empty data object

What it means

useCreate rejects mutations whose params.data is null or undefined, because a create call without payload data is meaningless. The check happens before the data provider is invoked.

Source

Thrown at packages/ra-core/src/dataProvider/useCreate.ts:115

        onSettled,
        ...mutationOptions
    } = options;

    const dataProviderCreate = useEvent((resource: string, params) =>
        dataProvider.create<RecordType, ResultRecordType>(
            resource,
            params as CreateParams<RecordType>
        )
    );
    const customMutationFnWithDataProviderResult = async (
        resource: string | undefined,
        params: Omit<UseCreateMutateParams<RecordType>, 'resource'>
    ) => {
        if (resource == null) {
            throw new Error('useCreate mutation requires a resource');
        }
        if (params.data == null) {
            throw new Error(
                'useCreate mutation requires a non-empty data object'
            );
        }
        if (customMutationFn == null) {
            return dataProviderCreate(resource, params);
        }

        return {
            data: await customMutationFn({ resource, ...params }),
        };
    };

    const [mutate, mutationResult] = useMutationWithMutationMode<
        MutationError,
        CreateResult<ResultRecordType>,
        UseCreateMutateParams<RecordType>
    >(
        { resource, ...params },

View on GitHub (pinned to 051f511bb0)

Solutions

  1. Always pass a non-null data object: mutate('posts', { data: { title: 'Hi' } })
  2. Guard the mutation: if (record) mutate('posts', { data: record })
  3. Provide defaults in the form or hook so data is never empty

Example fix

// before
mutate('posts', { data: record }); // record may be undefined
// after
if (record != null) mutate('posts', { data: record });
Defensive patterns

Strategy: validation

Validate before calling

if (data == null) {
  throw new Error('Cannot create: data payload is empty');
}
mutate('posts', { data });

Type guard

const hasData = (p: { data?: unknown }): p is { data: Record<string, unknown> } => p.data != null && typeof p.data === 'object';

Try / catch

try {
  await mutate('posts', { data });
} catch (e) {
  if (/non-empty data object/.test(e.message)) {
    notify('Nothing to save: form data is empty', { type: 'warning' });
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling mutate(resource) or mutate(resource, {}) with no data field, or building the payload asynchronously and mutating before it is ready.

Common situations: Form submissions wired before state initialization; conditional code paths where the record object is still undefined at submit time.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30). Data as JSON: /api/errors/20d3aaf856fe41e7. Report an issue: GitHub.