[英]Ember RSVP Promise resolve always returns undefined value
我正在使用Ember-simple-auth並嘗試將數據從我的自定義身份驗證器返回到執行身份驗證的控制器中。
在我的authenticator / custom.js中:
authenticate(identification, password) {
return new Ember.RSVP.Promise(function (resolve, reject) {
var loginURL = 'https://domain.com/login';
var authObj = Ember.Object.create({"identity":identification,"password":password});
var hash = authObj.getProperties('identity', 'password');
var stringHash = JSON.stringify(hash);
var xhr = new XMLHttpRequest();
xhr.open("post", loginURL);
xhr.onreadystatechange = handler;
xhr.responseType = 'json';
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Format', 'JSON');
xhr.send(stringHash);
function handler() {
if (this.readyState === this.DONE) {
if (this.status === 200) {
console.log('response is: ',this.response); /// <-- returns good response data
resolve(this.response);
} else {
reject( 'failed with status: [' + this.status + ']');
}
}
}
在我的登錄控制器中:
authenticate() {
let { identification, password } = this.getProperties('identification', 'password');
var session = this.get('session');
session.authenticate('authenticator:custom', identification, password).then((reason) => {
console.log('failed login',reason);
});
}
},
但我真的希望能夠處理resolve
函數並從authenticate
promise獲得它的value
有效負載。
如果我改變.catch
到.then
響應函數調用成功,但總是有一個明確的值作為其有效載荷:
session.authenticate('authenticator:custom', identification, password).then(
(response) => {
console.log('response: ',response); //// <---- always 'undefined'
this.setUserWithData(response);
},
(reason) => {
this.set('Login failed: ',reason);
}
);
}
即使重構承諾,重新調整函數的調用方式,也可以成功調用RSVP中的第一個函數,但具有未定義的有效負載。 RSVP的第二個函數始終具有正確的有效負載。
我試過反轉解決/拒絕:
Ember.RSVP.Promise(function (resolve, reject){
至
Ember.RSVP.Promise(function (reject, resolve){
並且該函數成功地執行了響應,但simple-auth現在認為它未能通過授權。
我希望能夠將響應有效負載傳遞給我的控制器。 或者,如果無法完成,我如何將響應中的數據注入到我的會話和ember-data存儲中? 從身份驗證器的身份驗證功能中調用數據並將數據插入到存儲中似乎不是一種好的做法。
會話的authenticate
方法不會使用值解析。 查看API文檔: http : //ember-simple-auth.com/api/classes/SessionService.html#method_authenticate
為了處理來自認證路由的響應,我使用函數sessionAuthenticated
來處理返回的數據。
因此, authenticators/custom.js
的authenticate
函數
authenticate(identification, password) {
return new Ember.RSVP.Promise(function (resolve, reject) {
var loginURL = 'https://domain.com/login';
var authObj = Ember.Object.create({"identity":identification,"password":password});
var hash = authObj.getProperties('identity', 'password');
var stringHash = JSON.stringify(hash);
var xhr = new XMLHttpRequest();
xhr.open("post", loginURL);
xhr.onreadystatechange = handler;
xhr.responseType = 'json';
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Format', 'JSON');
xhr.send(stringHash);
function handler() {
if (this.readyState === this.DONE) {
if (this.status === 200) {
console.log('200 response: \r',this.response);
resolve(this.response);
} else {
console.log('failure response',this.response);
reject(this.response);
}
}
}
});
},
sessionAuthenticated()
事件發生在routes/application.js
:
sessionAuthenticated: function(){
var session = this.get('session');
var data = session.get('data');
var response = data.authenticated;
console.log('sessionAuthenticated()',response);
},
sessionInvalidated: function(){
console.log('sessionInvalidated()',response);
},
在sessionAuthenticated
函數中, response
變量包含從authenticate(response)
內部的authenticate authenticate(response)
傳遞給它的所有信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.