每次点击登录按钮时都会出错
TypeError: Cannot read property 'then' of undefined
,但是重新加载后错误消失了。我能知道发生了什么吗?这是我的登录代码
onSignIn(e){
e.preventDefault()
this.Auth.login(this.state.signInUsername, this.state.signInPassword)
.then(res => {
this.props.history.replace('/')
})
.catch(err => {
alert(err)
})
}
这是我的验证码:
login(username, password){
axios.post('http://localhost:3000/user/login', {
username,
password
})
.then(this._checkStatus)
.then(res => {
if(res.data.success === true){
const payload = {
name: username,
}
this.setToken(payload)
return Promise.resolve(res)
}else{
console.log(res.data.message)
}
})
.catch(err => {
return Promise.reject(err)
})
}
答案 0 :(得分:1)
从登录功能返回axios。
login(username, password){
return axios.post('http://localhost:3000/user/login', {
username,
password
})
.then(this._checkStatus)
.then(res => {
if(res.data.success === true){
const payload = {
name: username,
}
this.setToken(payload)
return res;
}else{
console.log(res.data.message)
}
})
.catch(err => {
throw err;
})
}
答案 1 :(得分:0)
我相信您的axios.post使用-two-“ .then”。axios网站上提供的示例使用
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
每个axios调用只有一个。