DELETE请求在GET请求之前完成,当尝试在ember中findRecord和destroyRecord时 [英] DELETE request is completing before GET request when trying to findRecord and destroyRecord in ember
问题描述
从ember导入Ember;
导出默认值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');
}
}
});
我有一个注销按钮,具有hbs模板代码:
< li> {{#link-to'sessions.delete'}} {{fa-iconfa-lock}}注销{{/到}}< /锂>
Route.extend
代码执行时/ sessions / delete被调用!
我观察到的是REST调用的顺序是:
1)*** DELETE *** http://dozee.me:3000/api/sessions/<sessionId>
2)*** GET *** http://dozee.me:3000/api/sessions/<sessionId>
第二个REST请求引发和错误,因为那时session / cookie已经被清理了。 / p>
我期望的是,REST调用的顺序实际上应该是相反的,但是我无法弄清楚为什么不是这样!
任何指针&提示将不胜感激;)
这可能是因为Ember已经在该商店上有该会话并将该对象返回给您尝试通过进行GET调用来更新对象。
可以在删除后发生,因为承诺已经通过存储对象解决。
你可以做的是首先使用peekRecord查找会话对象,并且只有在store中找不到对象才能使findRecord调用。
其他方式将会成为一个被迫的服务器调用findRecord的会话,不会得到解决,直到对象从服务器返回。
希望这可以帮助你。
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 ;)
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.
这篇关于DELETE请求在GET请求之前完成,当尝试在ember中findRecord和destroyRecord时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!