Javascript 登录后的简单身份验证转换
根据文档中的示例,我的应用程序路径上有登录代码,但对身份验证的调用似乎并没有返回承诺。我在“then”中得到的响应是未定义的。因此,过渡不起作用。我必须手动刷新页面,然后调用顶部重定向Javascript 登录后的简单身份验证转换,javascript,ember.js,ember-simple-auth,Javascript,Ember.js,Ember Simple Auth,根据文档中的示例,我的应用程序路径上有登录代码,但对身份验证的调用似乎并没有返回承诺。我在“then”中得到的响应是未定义的。因此,过渡不起作用。我必须手动刷新页面,然后调用顶部重定向 import Ember from 'ember'; // Make 'session' available throughout the application import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mi
import Ember from 'ember';
// Make 'session' available throughout the application
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
redirect: function () {
this.transitionTo('orders');
},
actions: {
authenticate: function () {
var data = {
identification: this.controller.get('identification'),
password: this.controller.get('password')
};
this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', data).then(
function(response) {
console.log(response); // undefined
this.transitionTo('orders'); // can't call on undefined
}
);
},
}
});
会话的
authenticate
方法返回的承诺没有解析为值。您可以通过会话的secure
属性访问验证器解析的数据,例如this.get('session.secure.token')
我的问题是函数调用中的“this”是错误的对象。使用var_this=this求解
我将发布完整的工作代码
import Ember from 'ember';
// Make 'session' available throughout the application
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
redirect: function () {
this.transitionTo('orders');
},
actions: {
authenticate: function () {
var data = {
identification: this.controller.get('identification'),
password: this.controller.get('password')
};
var _this = this;
this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', data).then(
function(response) {
console.log(_this.get('session')); // this correctly gets the session
_this.transitionTo('orders');
}
);
},
}
});
Marco我现在明白了,函数中的“this”具有错误的上下文。我已经发布了完整的工作代码,以防对某人有所帮助。谢谢你的帮助,昨晚这件事让我挠头很久了!您应该改为使用箭头函数,这样就不必为此分配额外的
\u
。关键是您在回调中使用了一个response
参数,它总是未定义的。