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

useInitializeFormWithRecord Error #4826

Closed
wmwart opened this issue May 18, 2020 · 1 comment
Closed

useInitializeFormWithRecord Error #4826

wmwart opened this issue May 18, 2020 · 1 comment

Comments

@wmwart
Copy link

wmwart commented May 18, 2020

What you were expecting:
expected data to be displayed inside <ReferenceArrayInput> c < AutocompleteArrayInput / >

What happened instead:
The application crashes

Steps to reproduce:
went into debugging and found a file with an error http://localhost:3001/static/js/C:/_Projects/ASKUG/web-askug-service-portal/node_modules/ra-core/esm/form/useInitializeFormWithRecord.js

Related code:

import { useEffect } from 'react';
import { useForm } from 'react-final-form';
/**
 * Restore the record values which should override any default values specified on the form.
 */
var useInitializeFormWithRecord = function (record) {
    var form = useForm();
    useEffect(function () {
        if (!record) {
            return;
        }
        var registeredFields = form.getRegisteredFields();
        // react-final-form does not provide a way to set multiple values in one call.
        // Using batch ensure we don't get rerenders until all our values are set
        form.batch(function () {
            Object.keys(record).forEach(function (key) {
                // We have to check that the record key is actually registered as a field
                // as some record keys may not have a matching input
                if (registeredFields.some(function (field) { return field === key; })) {
                    if (Array.isArray(record[key])) {
                        // array of values
                        record[key].forEach(function (value, index) {

///ERROR HERE
for record = {
tagsIds: []
tagsIds: ["ck88peh0401av0771i43t3kw9", "ck7yrq9sk00kc07683mntfi5j"]
}

value = "ck88peh0401av0771i43t3kw9" - scalar value, not array of objects
(value && Object.keys(value).length > 0) ==> true!



                            if (value && Object.keys(value).length > 0) {
                                // array of objects
                                Object.keys(value).forEach(function (key2) {
                                    form.change(key + "[" + index + "]." + key2, value[key2]);
                                });
                            }
                            else {
                                // array of scalar values
                                form.change(key + "[" + index + "]", value);
                            }
                        });
                    }
                    else {
                        // scalar value
                        form.change(key, record[key]);
                    }
                    form.resetFieldState(key);
                }
            });
        });
    }, [form, JSON.stringify(record)]); // eslint-disable-line react-hooks/exhaustive-deps
};
export default useInitializeFormWithRecord;

Codesandbox with error
Pay attention to the file data.js, where the object id can be text

 posts: [
    {
      id: 1,
     ...
      tags: ["a1", "a2"],
...
      notifications: ["a1", "a2", "a3"]
    },

Environment

  • React-admin version: 3.5.2
  • Last version that did not exhibit the issue (if applicable): 3.5.0
@alanpoulain
Copy link
Contributor

Fixed by #4819.

@wmwart wmwart closed this as completed May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants