I'm sorry I don't have a reproducible case of this, but I was hoping you could help me interpret what this error message means so I could diagnose/fix it on my end:
ember.debug.js:32096 Error: Attempted to handle event `notFound` on <MYAPP@model:comment::ember1160:18> while in state root.deleted.inFlight.
at EmberError (ember.debug.js:16581)
at InternalModel._unhandledEvent (internal-model.js:502)
at InternalModel.send (internal-model.js:379)
at InternalModel.notFound (internal-model.js:283)
at finders.js:41
at tryCatch (ember.debug.js:53806)
at invokeCallback (ember.debug.js:53821)
at publish (ember.debug.js:53789)
at publishRejection (ember.debug.js:53724)
at ember.debug.js:32054onerrorDefault @ ember.debug.js:32096trigger @ ember.debug.js:54476(anonymous function) @ ember.debug.js:55727invoke @ ember.debug.js:333flush @ ember.debug.js:397flush @ ember.debug.js:205end @ ember.debug.js:560run @ ember.debug.js:682join @ ember.debug.js:702run.join @ ember.debug.js:21181hash.error @ rest.js:916fire @ jquery.js:3148fireWith @ jquery.js:3260done @ jquery.js:9316callback @ jquery.js:9718
This is outputting after this call executes:
store.find('comment', comment.id).then(function (c) {
if (currentUser.id === c.user.id) {
c.destroyRecord({
adapterOptions: {
parentType: parentType,
parentId: parentId,
method: 'delete'
}
});
}
});
For context, my comment model always makes use of an option hash being handed to its adapter (to deal with a non-standard endpoint on my API). In this case, I'm deleting the comment, which is successful, but then I see this error. My app appears to function totally normally, but I figure I must be doing something wrong given I see this output in console.
Hey!
It looks like you're running into an issue with background reloading:
The promise returned by store.find("comment", comment.id) resolves immediately, since the comment is already in the store. Then, within the then handler you call comment.destroyRecord which, triggers a DELETE /comments/:id.
The thing is that a background reload for the comment is triggered via the store.find. This is a functionality in ember data so you immediately get the record with the current state in the store and then the data for the record is, well, updated in the background.
It looks like by the time the background reload is triggered, your comment is already deleted from the server and I guess a 404 is returned. That's why the notFound event is triggered on the record, which is in the root.deleted.inFlight state. This state doesn't know how to handle 404s since it basically only handles the acknowledgement of the deletion.
Long story short, background reload bites you here. I propose the following way of deleting the comment:
// get the comment from the store synchronously, without triggering a fetch
let comment = store.peekRecord("comment", commentId);
// check if there is a comment; is null in the case the store has no comment with the given id
if (comment) {
comment.destroyRecord(...);
}
If you don't know if the comment is in the store but you definitely want to make sure it is deleted from the server, you can use the following approach:
// find the record (trigger adapter call if not yet loaded in store), but prevent a background reload
store.findRecord("comment", commentId, {聽backgroundReload: false }).then(function(comment) {
comment.destroyRecord();
});
You can find further documentation here.
Also note that store.find is deprecated and you should use the public store.findRecord instead.
I hope this helped. Feel free to reopen if you are still running into this issue and this didn't help.
Most helpful comment
Hey!
It looks like you're running into an issue with background reloading:
The promise returned by
store.find("comment", comment.id)resolves immediately, since the comment is already in the store. Then, within thethenhandler you callcomment.destroyRecordwhich, triggers aDELETE /comments/:id.The thing is that a background reload for the comment is triggered via the
store.find. This is a functionality in ember data so you immediately get the record with the current state in the store and then the data for the record is, well, updated in the background.It looks like by the time the background reload is triggered, your comment is already deleted from the server and I guess a 404 is returned. That's why the
notFoundevent is triggered on the record, which is in theroot.deleted.inFlightstate. This state doesn't know how to handle 404s since it basically only handles the acknowledgement of the deletion.Long story short, background reload bites you here. I propose the following way of deleting the comment:
If you don't know if the comment is in the store but you definitely want to make sure it is deleted from the server, you can use the following approach:
You can find further documentation here.
Also note that
store.findis deprecated and you should use the publicstore.findRecordinstead.I hope this helped. Feel free to reopen if you are still running into this issue and this didn't help.