emberjs/data

model.unloadRecord causes Uncaught TypeError: Cannot read property 'eachAttribute' of null

Closed this issue ยท 10 comments

at new Snapshot (-private.js:5069)
    at InternalModel.createSnapshot (-private.js:6894)
    at Class._flushPendingFetchForType (-private.js:10771)
    at cb (ember-metal.js:5546)
    at OrderedSet.forEach (ember-metal.js:5358)
    at MapWithDefault.forEach (ember-metal.js:5554)
    at Class.flushAllPendingFetches (-private.js:10687)
    at invoke (backburner.js:258)
    at Queue.flush (backburner.js:151)

This is after calling findRecord then unloadRecord on the result. We are upgrading to ember 2.18 and ember-data 2.18, and this appears to be a regression.

Twiddle reproduction:
https://ember-twiddle.com/c16533267ff13353b25498fac4d05ca3?fileTreeShown=false&numColumns=2&openFiles=controllers.application.js%2Ctemplates.application.hbs

Possibly a duplicate of #4963, but that was supposedly fixed a while ago.

The code that doesn't work is this:

this.get('store').findRecord('person', 1).then((person) => {
   person.unloadRecord();
});
jlami commented

Unloading the record from the controller.model seems to not give this error.
It actually really needs to be just after the findRecord.
I also have this problem in 2.17.

In my case I found out that it is triggered due to a ManyRelationship.findRecords() when one of the items in the relationship is being dematerialized. Maybe store._scheduleFetch should detect internalModel._isDematerializing and don't do anything?

Repoduced in ember-data 3.0.1

I have the same problems. If I do that it works :

record.destroyRecord().then(
	() => store.unloadRecord(record)
)

I tried to trace the code, and apparently, unloadRecord somehow schedule a fetch before the object is destroyed. I think it is because the hasMany relationship is changed ( record I'm trying to unload has a belongsTo ).

I get the same error, when I try to load a relationship by ID, that 404s.

For instance:

{
  id: '1',
  type: 'post',
  relationships: {
    author: { id: '2', type: 'author' }
  }
}

Now, if I access the author relationship and therefore trigger Ember Data to load it and the request fails with a 404, I get the same error:

Uncaught TypeError: Cannot read property 'eachAttribute' of null
    at new Snapshot (vendor.js:181070)
    at InternalModel.createSnapshot (vendor.js:182969)
    at Class._flushPendingFetchForType (vendor.js:186533)
    at cb (vendor.js:45496)
    at OrderedSet.forEach (vendor.js:45309)
    at MapWithDefault.forEach (vendor.js:45504)
    at Class.flushAllPendingFetches (vendor.js:186450)
    at invoke (vendor.js:27457)
    at Queue.flush (vendor.js:27377)
    at DeferredActionQueues.flush (vendor.js:27530)

To make things worse, this appears to put Ember Data in an invalid state, that requires a page reload.

I've had issues similar to this with all releases after 2.12.2 and therefore have not been able to upgrade ember-data past that release

I've got an app on 3.0 and can consistently reproduce this error. Would be happy to help debug if anyone wants to provide me with some direction.

@wagenet There are two failing tests in #5353 and #5354, maybe a good starting point ?

Thanks for the detailed report and twiddle reproduction! (seriously that helps a lot and is much appreciated)

I believe #5376 solves this (it essentially implements @jlami's suggestion). Please let me know if you're still able to reproduce this with those changes.

Note that #5353 is a different issue (see #5377 for fix)

@hjdivad Thanks for the PR - looks like the problem is fixed in our app using hjdivad/data#1ae51810efee171f260cabf4c725e4abcaf1dfb8
as compared to ember-data ~2.18.0 ๐Ÿ‘

I'm having the exact same issue as @buschtoens mentioned here.

Any idea how to work around it?