data icon indicating copy to clipboard operation
data copied to clipboard

belongsTo property has no id after GET request is rejected

Open al3xnag opened this issue 4 years ago • 6 comments

Description

// RESTAdapter is used
export default class MyModel extends Model {
  @belongsTo('foo', { async: true }) foo;
}

const myModel = loadMyModel(); // myModel is loaded, myModel.foo is not loaded.
myModel.belongsTo('foo').id() // 99
Ember.get(myModel, 'foo.id'); // logs 99 and initiates 'foo' loading
...
// Backend rejects the request `/api/foos/99` with 403.
...
Ember.get(myModel, 'foo.id'); // logs undefined <----------------- expected 99.
myModel.belongsTo('foo').id() // 99

I'm not sure if it's a bug, but at least it's very strange.

Versions

ember-data 3.24.0

al3xnag avatar Jul 09 '21 12:07 al3xnag

@nag5000 Is this something you would be interested in pushing a PR with a test indicating your issue? That way we can prioritize a fix.

snewcomer avatar Sep 19 '21 03:09 snewcomer

@snewcomer Yup, just added.

al3xnag avatar Sep 19 '21 10:09 al3xnag

@nag5000 Thank you for the test. So this isn't expected from the point of view of a the ember primitives - ObjectProxy and PromiseProxyMixin. However, in the comments in the code below...

when a load fails, in this case we need to make sure that we aren't proxying to destroyed content

https://github.com/emberjs/data/blob/9889ebbd4eec162dba17bb00126ee71e49d04bce/packages/store/addon/-private/system/model/internal-model.ts#L1396-L1407

This is unavoidable. Unless you are able to convert to non async relationships. Seems this issues occurs if we were to revert null'ing out the content of the proxy after an error.

https://github.com/emberjs/data/issues/5814

snewcomer avatar Oct 20 '21 02:10 snewcomer

Let me know if you have any other questions or concerns. Will close the issue for now.

snewcomer avatar Oct 20 '21 03:10 snewcomer

Yeah, I see. To be honest, it still seems odd.., but, yeah, I understand it's by design, and it doesn't make sense to make a lot of changes to the InternalModel to fix this.

To clarify things a bit, this kinda "inconsistency" in a real-world application can lead to some oddities when the id of an async belongsTo relationship is used as a dependency key in a computed property, or as a model in LinkTo <LinkTo @route="a" @model={{this.myModel.foo.id}}>{{this.myModel.foo.title}}</LinkTo>. It's not very critical, but sometimes you have to reckon with it.

al3xnag avatar Oct 20 '21 07:10 al3xnag

I'm not sure this should be closed, so I'm reopening it. While the current behavior is indeed intended, it's open for discussion whether there isn't a better way to handle this case (where we know the id, but for unknown reasons the API request is failing). Potentially we can enter a recoverable error state here.

runspired avatar Dec 15 '21 09:12 runspired

closing as a duplicate of #5229

runspired avatar Sep 10 '23 09:09 runspired