Gutenberg: Using saveEntityRecord throws a rest_no_route error.

Created on 3 Oct 2019  路  13Comments  路  Source: WordPress/gutenberg

Describe the bug
Using saveEntityRecord throws a rest_no_route error. I only tested this saving posts, they are saved regardless although I only tried with basic data. I found this other bug with the REST API, which I think could be related to this one.

To reproduce
Steps to reproduce the behavior:

  1. Go to a post editor where Gutenberg is enabled.
  2. Open the console and execute

wp.data.dispatch('core').saveEntityRecord( 'postType', 'post', { title: 'new post' } )

  1. See error

Version

  • WordPress 5.2.3
  • Gutenberg 6.6.0

Thanks

REST API Interaction [Package] Core data [Type] Bug

All 13 comments

Hi @garciaalvaro, the problem is that { title: 'new post' } is not a valid post entity, it's missing .id.

The REST API should probably throw a more descriptive error.

Thanks. Maybe I've been missusing the action until now but I thought if no id was given then a new post was created. This works with the latest WP version.

Are you saying it used to work and now it has regressed, or that it was fixed in the last 5 days?

If I run wp.data.dispatch('core').saveEntityRecord( 'postType', 'post', { title: 'new post' } ) in WordPress 5.2 or 5.1 (with no external Gutenberg plugin) it saves a post with draft status. No errors.

Using the current plugin version 6.6.0 (and at least the previous version too) it throws the error although it saves the post. But I didnt try saving the post with more props (apart from title) so cant confirm it saves correctly all the data passed.

Thanks for the details, sounds like something changed with the REST API.

Does anything come to mind? @swissspidy @TimothyBJacobs

It seems more like a change in the package. Before saving the record it tries to load the current record from the API, which obviously fails because no entry exists for a record with id being undefined.

Can you post a screenshot of the actual API call that gets made in your browser's dev tools?

rest_no_route only gets thrown when there isn't a found route at all, we don't send it if request data is invalid.

I'm not aware of any REST API changes to this area.

Screenshots:

Screenshot 2019-10-08 at 15 11 38
Screenshot 2019-10-08 at 15 11 34
Screenshot 2019-10-08 at 15 11 26
Screenshot 2019-10-08 at 15 11 18

Here's where I think the error happens:

https://github.com/WordPress/gutenberg/blob/f583ff6eb7c0a3e200779dbc9785bea8fa34ea6f/packages/core-data/src/actions.js#L259-L263

recordId is not validated. And getRawEntityRecord causes a REST API call if I'm not mistaken.

Right, but the error is swallowed and everything works as expected. It just logs the 404 in the console.

We can't change it to skip that step if recordId === undefined because some entities like the site entity, won't have a recordId and the API's response will be ok.

Should we just ignore the 404?

There is already some logic in there specific to the postType kind, so we could skip if recordId === undefined only for that kind.

We would have to tag every kind with that behavior. I would rather avoid adding that complexity.

Still having this issue. It probably throws the rest_no_route error because the route for the entity lookup is probably defined with a pattern to only match integers. Obviously, "undefined" is not an integer, so it makes sense no matching route was found.

Was this page helpful?
0 / 5 - 0 ratings