{"record":{"id":"c9f8c85fa73f1d8b","repo":"marmelab/react-admin","slug":"usecreate-mutation-requires-a-resource","errorCode":null,"errorMessage":"useCreate mutation requires a resource","messagePattern":"useCreate mutation requires a resource","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/dataProvider/useCreate.ts","lineNumber":112,"sourceCode":"        mutationMode = 'pessimistic',\n        getMutateWithMiddlewares,\n        mutationFn: customMutationFn,\n        onSettled,\n        ...mutationOptions\n    } = options;\n\n    const dataProviderCreate = useEvent((resource: string, params) =>\n        dataProvider.create<RecordType, ResultRecordType>(\n            resource,\n            params as CreateParams<RecordType>\n        )\n    );\n    const customMutationFnWithDataProviderResult = async (\n        resource: string | undefined,\n        params: Omit<UseCreateMutateParams<RecordType>, 'resource'>\n    ) => {\n        if (resource == null) {\n            throw new Error('useCreate mutation requires a resource');\n        }\n        if (params.data == null) {\n            throw new Error(\n                'useCreate mutation requires a non-empty data object'\n            );\n        }\n        if (customMutationFn == null) {\n            return dataProviderCreate(resource, params);\n        }\n\n        return {\n            data: await customMutationFn({ resource, ...params }),\n        };\n    };\n\n    const [mutate, mutationResult] = useMutationWithMutationMode<\n        MutationError,\n        CreateResult<ResultRecordType>,","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/dataProvider/useCreate.ts#L94-L130","documentation":"useCreate's internal mutation function validates that a resource string was provided before calling dataProvider.create. Since the resource normally comes from the hook argument or useResourceContext, calling mutate without a resolvable resource makes creation impossible.","triggerScenarios":"Calling the mutate/callback returned by useCreate() (e.g. mutate({ data })) when useCreate was called with no resource and no resource is available from the routing/context.","commonSituations":"Using useCreate() outside of a resource context (not inside a Resource route) and omitting the resource argument; typos in resource naming after refactors.","solutions":["Pass the resource explicitly: mutate('posts', { data: {...} })","Call useCreate('posts') with a resource argument","Ensure the component is rendered within a <Resource> / matched route so useResourceContext resolves"],"exampleFix":"// before\nconst { mutate } = useCreate();\nmutate({ data: { title: 'Hi' } });\n// after\nconst { mutate } = useCreate('posts');\nmutate('posts', { data: { title: 'Hi' } });","handlingStrategy":"validation","validationCode":"const resource = resourceFromContext ?? 'posts';\nif (resource == null) {\n  throw new Error('useCreate requires a resource; pass one to the hook or mutate()');\n}","typeGuard":"const hasResource = (r: string | undefined | null): r is string => typeof r === 'string' && r.length > 0;","tryCatchPattern":"try {\n  await mutate('posts', { data });\n} catch (e) {\n  if (e.message === 'useCreate mutation requires a resource') {\n    // fall back to explicit resource or surface a config error\n  }\n  throw e;\n}","preventionTips":["Always pass the resource as the first hook argument instead of relying on context","TypeScript: type the resource parameter as string, not string | undefined","Render mutation components inside a Resource route or pass resource as a prop"],"tags":["react-admin","usecreate","mutation","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}