DELETE request is completing before GET request when trying to findRecord and destroyRecord in ember

烂漫一生 提交于 2019-12-12 02:37:49

问题


I have the following code in ember 2.6; ember-data 2.6.1

import Ember from 'ember';

export default Ember.Route.extend({
    activate() {
        var route = this;
        var sessionId = localStorage.getItem('sessionId');
        if (sessionId) {
            localStorage.removeItem('sessionId');
            this.store.findRecord('session', sessionId).then(function found(session) {
                session.destroyRecord();
                route.transitionTo('index');
            });
        } else {
            this.transitionTo('sessions.new');
        }
    }
});

I have a logout button that has hbs template code as:

<li>{{#link-to 'sessions.delete'}}{{fa-icon "fa-lock"}} Logout{{/link-to}}</li>

The Route.extend code is executed when /sessions/delete is called!

What I observe is the order of REST calls are:

1) ***DELETE*** http://dozee.me:3000/api/sessions/<sessionId>
2) ***GET*** http://dozee.me:3000/api/sessions/<sessionId>

The second REST request throws and error because by that time the session/cookie is already cleaned up.

What I expect is the order of the REST calls should actually be reverse but I am unable to figure out why is it not like that!

Any pointers & hints will be appreciated ;)


回答1:


this might be because ember already have that session on the store and returning that object to you and trying to update the object later via making a GET call.

which can happen after delete because promise was already resolved with store object.

What u can do is look for session object in store first with peekRecord and make findRecord call only if object in not found in store.

Other way would be make a forsed server call for findRecord for session which wont get resolved untill object is returned from server.

Hope this helps you.



来源:https://stackoverflow.com/questions/38424030/delete-request-is-completing-before-get-request-when-trying-to-findrecord-and-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!