我试图将值设置为userSessionStorage
,当我从authenticate()函数访问它时,它似乎正常工作。ember:TypeError:无法读取未定义的属性'set'
但是,它在.then()承诺中不起作用。
应用程序/控制器/ show.js
import Ember from 'ember';
import { storageFor } from 'ember-local-storage';
export default Ember.Controller.extend({
session: Ember.inject.service('session'),
userSessionStorage: storageFor('user'),
authenticator: 'authenticator:custom',
actions: {
authenticate: function() {
var credentials = this.getProperties('identification', 'password');
// ok
this.set('userSessionStorage.username', credentials.identification);
this.get('session').authenticate('authenticator:custom', credentials)
.then(function(){
// error: TypeError: Cannot read property 'set' of undefined
this.set('userSessionStorage.username', credentials.identification);
})
.catch((message) => {
console.log("message: " + message);
this.controller.set('loginFailed', true);
});
}
}
});
将您传递给'then'的函数重写为保持'this'周围值的箭头函数。或者用'const self = this;'在'authenticate'方法的顶部捕获'this'的这个值,然后使用'self.set'。 – 2017-04-23 04:47:48
@torazaburo你很好:)你可以请把这个作为答案,以便我可以把它作为正确的答案? –
为什么不在你的验证器中设置'userSessionStorage.username'? –