-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
v 3.0.0 Unexpected token u in JSON (json parse undefined) when ../create/?someparams #4031
Comments
ok here is the problem |
Why are you passing GET parameters to the create route? |
I have the same issue. The reason why I'm passing the GET parameters is to filter or pre-fill some fields like in this tutorial: https://marmelab.com/blog/2018/07/09/react-admin-tutorials-form-for-related-records.html |
Just ran into this and found the culprit. It's a bug caused by the new You can fix this for now by passing an encoded empty object as source, like so It's possible to piggyback off this feature to get the functionality you want @akbkk, by encoding your pre-filled form field as a stringified JSON object passed via |
const AddNewCommentButton = ({ record }) => (
<Button
component={Link}
to={{
pathname: "/comments/create",
search: `?post_id=${record.id}`,
}}
label="Add a comment"
>
<ChatBubbleIcon />
</Button>
); |
Well, trying to use the 'source' parameter resulted in some strange bugs, so I had to do this for every Create screen. It's pretty inconvenient though. const {classes, location, ...props} = this.props
const { var1, var2, redirect } = parse(location.search)
const createLocation = {...location}
createLocation.search = ''
const initial = { var1, var2 }
return (
<Create classes={classes} {...props} location={createLocation}>
<SimpleForm redirect={redirect} initialValues={initial}> |
Also having this same issue, also using url params to pre-fill some fields. |
My CloneButton allows for a callback that is used to 'tweak' the record being cloned. That way you can have a mechanism to clear or alter in some way fields from the source record. This might alleviate the need to pass params -- which is likely interfering with the clone functionality. I think it would be cool to have an optional 'tweakSourceRecordCallback' parameter :) |
In 3.0, the way to pre-fill some fields in the create form is to pass a stringified object as the export const CloneButton = ({
basePath = '',
label = 'ra.action.clone',
record = {},
icon = <Queue />,
...rest
}) => (
<Button
component={Link}
to={{
pathname: `${basePath}/create`,
search: stringify({ source: JSON.stringify(omitId(record)) }),
}}
label={label}
onClick={stopPropagation}
{...sanitizeRestProps(rest)}
>
{icon}
</Button>
); In v2, this feature didn't use the fields of the The advanced tutorials were indeed written for v2. For v3, the right syntax should be: const AddNewCommentButton = ({ record }) => (
<Button
component={Link}
to={{
pathname: "/comments/create",
- search: `?post_id=${record.id}`,
+ search: `?source=${JSON.stringify({ post_id: record.id })}`,
}}
label="Add a comment"
>
<ChatBubbleIcon />
</Button>
); Sorry for not mentioning it in the UPGRADE guide - we figured that, since both the So this is a breaking change, not a bug. I'll document it in the Upgrade guide. Also, there is indeed a small bug: |
I also just discovered another, seemingly more intended, way to do this: Using const AddNewCommentButton = ({ record }) => (
<Button
component={Link}
to={{
pathname: "/comments/create",
state: { post_id: record.id },
}}
label="Add a comment"
>
<ChatBubbleIcon />
</Button>
); |
Great stuff! |
Steps to reproduce:
#/resource/create?anyParams
Unexpected token u in JSON at position 0
seems json is trying to parse undefined variable
Environment
The text was updated successfully, but these errors were encountered: