Directory

Regression: Error when using nested properties in `_fields` with `getEntityRecord()` · Issue #54735 · WordPress/gutenberg · GitHub
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

Regression: Error when using nested properties in _fields with getEntityRecord() #54735

Closed
kraftner opened this issue Sep 22, 2023 · 3 comments · Fixed by #54790
Closed

Regression: Error when using nested properties in _fields with getEntityRecord() #54735

kraftner opened this issue Sep 22, 2023 · 3 comments · Fixed by #54790
Assignees
Labels
[Package] Core data /packages/core-data [Status] In Progress Tracking issues with work in progress [Type] Regression Related to a regression in the latest release

Comments

@kraftner
Copy link

Description

When requesting something from the REST API using wp.data.select('core').getEntityRecord() this can result in an uncaught Type Error.

I noticed this when trying to get back the routes field removed in https://github.com/WordPress/gutenberg/pull/39256/files#diff-38a74d088ea018dbbea00d8f5bd5c03ea54bfc099d1d1836686c24a5582bf579R27-R39 using the _fields parameter.

Here is an example. Just run this code in the console when editing a post:

wp.data.select('core').getEntityRecord()'root', '__unstableBase', undefined, { _fields: [ 'routes./wp/v2/posts' ] })

Yes, I know, I am using __unstableBase here which is a private API. But please still hear me out since the surfaced issue is a fundamental bug that probably can also occur in other situations. I just wasn't able to find another example quickly now.


I think this issue appeared when removing Lodash _.get() in 4b12c75#diff-6fae435c7327c5edb1a67718054b7ee8513d4691a19b416e7d5f628aefc4459bR69-R72 of #48310.

The problem is that using the dot syntax to limit the retrieved data to a nested property tries to get a property of undefined here when first requesting the data:

which fails with the Type Error mentioned above.

This is also confirmed by the fact that the following runs flawlessly:

wp.data.select('core').getEntityRecord('root', '__unstableBase', undefined, { _fields: [ 'routes' ] })

The old implementation with Lodash _.get()

const value = get( item, field );

though would handle this edge case returning undefined instead of causing a Type Error in both cases.


So as a solution I'd assume an additional check for undefined would solve this issue. Something along the lines of

value = typeof value === 'undefined' ? undefined: value[fieldName];

Which a quick check altering WP 6.3 core confirmed.

Step-by-step reproduction instructions

  1. Open the block editor

  2. Run wp.data.select('core').getEntityRecord()'root', '__unstableBase', undefined, { _fields: [ 'routes./wp/v2/posts' ] }) in the console

  3. See a "Uncaught TypeError: value is undefined".

  4. Open the block editor (reload, doesn't work when the data has been requested before)

  5. Run wp.data.select('core').getEntityRecord()'root', '__unstableBase', undefined, { _fields: [ 'routes' ] }) in the console

  6. See no error but a value of undefined returned.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.3
  • no Gutenberg

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Mamaduka Mamaduka added [Type] Regression Related to a regression in the latest release [Package] Core data /packages/core-data labels Sep 22, 2023
@Mamaduka
Copy link
Member

@tyxla, do you have time to look into this?

@tyxla
Copy link
Member

tyxla commented Sep 25, 2023

Thanks for the thorough report @kraftner and for the ping @Mamaduka.

I'm happy to take a look this week.

@tyxla
Copy link
Member

tyxla commented Sep 25, 2023

Fix is in #54790.

@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Core data /packages/core-data [Status] In Progress Tracking issues with work in progress [Type] Regression Related to a regression in the latest release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants