[英]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.