Li Xinyang Li Xinyang - 4 years ago 167
Javascript Question

Error while destroy Ember Data record

I have encounterd an error while deleting an Ember data record and I could not figure out why,

Attempted to handle event `pushedData` on <card:32> while in state root.deleted.inFlight.
Error


The code below is where the error get triggered,

deleteBtnClicked() {
const controller = this.controllerFor('browse/index');
let selectedCards = controller.getSelectedCard();
selectedCards = selectedCards.map((card) => {
return parseInt(Ember.get(card, 'id'));
});

selectedCards.forEach((id) => {
this.store.find('card', id)
.then((card) => {
card.destroyRecord();
});
});
}


Turns out the records have been deleted successfuly but the error persists.

Answer Source

find is private method so please don't use it. Instead you can use findAll and findRecord

deleteBtnClicked() {
    const controller = this.controllerFor('browse/index');
    let selectedCards = controller.getSelectedCard();
    //if selectedCards itself is RecordArray you can easily iterate using forEach and destroyRecord. no need of findRecord.
    selectedCards = selectedCards.map((card) => {
        return parseInt(Ember.get(card, 'id'));
    });
    //I added reload: true this will ensure promise will be resolved only after adapters returns the data.
    selectedCards.forEach((id) => {
        this.store.findRecord('card', id, { reload: true })
            .then((card) => {
                card.destroyRecord();
            });
    });
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download