Javascript ember:TypeError:无法读取属性';设置';未定义的
我正在尝试将一个值设置到Javascript ember:TypeError:无法读取属性';设置';未定义的,javascript,ember.js,Javascript,Ember.js,我正在尝试将一个值设置到userSessionStorage,当我从authenticate()函数访问它时,它似乎工作正常 但是,它在.then()承诺中不起作用 app/controllers/show.js import Ember from 'ember'; import { storageFor } from 'ember-local-storage'; export default Ember.Controller.extend({ session: Ember.inject.
userSessionStorage
,当我从authenticate()函数访问它时,它似乎工作正常
但是,它在.then()承诺中不起作用
app/controllers/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);
});
}
}
});
它基于我自己的代码,所以您可能需要修改它。但是在验证器中,
authenticate
函数可能如下所示:
# authenticator
authenticate(credentials) {
const { identification, password } = credentials;
const data = JSON.stringify({
auth: {
email: identification,
password
}
});
const requestOptions = {
url: this.tokenEndpoint,
type: 'POST',
data,
contentType: 'application/json',
dataType: 'json'
};
return new Promise((resolve, reject) => {
ajax(requestOptions).then((response) => {
// Set your session here
}, (error) => {
run(() => {
reject(error);
});
});
});
},
它基于我自己的代码,所以您可能需要修改它。但是在验证器中,
authenticate
函数可能如下所示:
# authenticator
authenticate(credentials) {
const { identification, password } = credentials;
const data = JSON.stringify({
auth: {
email: identification,
password
}
});
const requestOptions = {
url: this.tokenEndpoint,
type: 'POST',
data,
contentType: 'application/json',
dataType: 'json'
};
return new Promise((resolve, reject) => {
ajax(requestOptions).then((response) => {
// Set your session here
}, (error) => {
run(() => {
reject(error);
});
});
});
},
您只需更改以下行:
this.get('session').authenticate('authenticator:custom', credentials)
.then(function(){....}
要使用胖箭头符号,请执行以下操作:
this.get('session').authenticate('authenticator:custom', credentials)
.then(()=>{....}
因此,promise上下文中的
上下文将成为您的控制器。有关ES6箭头功能的更多信息,请参阅。您只需更改以下行:
this.get('session').authenticate('authenticator:custom', credentials)
.then(function(){....}
要使用胖箭头符号,请执行以下操作:
this.get('session').authenticate('authenticator:custom', credentials)
.then(()=>{....}
因此,promise上下文中的上下文将成为您的控制器。有关ES6 arrow函数的更多信息,请参阅。将要传递给的函数重写为一个arrow函数,该函数将保持此
的周围值。或者使用const self=this在authenticate
方法顶部捕获this
的该值编码>然后使用self.set
@torazaburo你很好:)你能把这个作为答案,这样我就可以把它作为正确的答案吗?为什么不在你的验证器中设置userSessionStorage.username
,也许你能把它作为一个答案吗?把你传递给的函数重写成一个箭头函数,它保持这个
的周围值。或者使用const self=this在authenticate
方法顶部捕获this
的该值编码>然后使用self.set
@torazaburo你很好:)你能把这个作为答案,这样我就可以把它作为正确的答案吗?为什么不在你的验证器中设置userSessionStorage.username
?@j-samah你能详细说明一下吗,也许你能把它作为答案吗?