我在我的网站上开发登录方法,但我在 login.controller.js 中遇到问题 我有这个错误:
TypeError:无法读取属性' message'未定义的。
at login.controller.js:35
'use strict';
export default class LoginController {
user = {
name: '',
email: '',
password: ''
};
errors = {
login: undefined
};
submitted = false;
/*@ngInject*/
constructor(Auth, $state) {
this.Auth = Auth;
this.$state = $state;
}
login(form) {
this.submitted = true;
if(form.$valid) {
this.Auth.login({
email: this.user.email,
password: this.user.password,
rememberme: this.user.remember
})
.then(() => {
// Logged in, redirect to home
this.$state.go('main');
})
.catch(err => {
this.errors.other = err.message;
});
}
}
}
答案 0 :(得分:0)
export default class LoginController {
/* @ngInject */
constructor(Auth, $state) {
this.Auth = Auth;
this.$state = $state;
this.user = {
name: null,
email: null,
password: null
};
this.errors = {
login: null,
others: null // This was needed;
};
this.submitted = false;
}
login(form) {
this.submitted = true;
if (form.$valid) {
this.Auth.login({
email: this.user.email,
password: this.user.password,
rememberme: this.user.remember
})
.then(() => {
return this.$state.go('main');
})
.catch((err) => {
console.log(err); // see what 'err' looks like here
this.errors.other = err.message;
})
.finally(() => {
// Logged in, redirect to home
console.log('last thing');
});
}
}
}
我将变量移动到构造函数中。放入console.log以查看错误的样子。错误可能不存在'消息'。还需要声明this.errors.others。