无法读取家庭上的令牌-授权:未定义承载



嗨,我正在登录表单中进行身份验证,在登录表单上输入正确的凭据后,我会得到令牌,但当我转到我的componentdidmount所在的Home组件时,给我错误,我的Authorization: Bearer undefined401 UnAuthorize

登录组件

handleSubmit = e => {
e.preventDefault();
const data ={
username:this.username,
password: this.password
}

axios.post('http://localhost:5000/api/auth',data)
.then(res=>{
console.log(res)
localStorage.setItem('token',res.data.token);
})
.catch(err=>{
console.log(err)
})
};

家庭组件

componentDidMount(){
const config ={
headers: {
Authorization: 'Bearer ' + localStorage.getItem('token')

}
};


axios.get('http://localhost:5000/api/user',config)
.then(
res=> {
console.log(res);
},
err =>{
console.log(err);
}

)
}

我看到res是一个带有密钥data的对象。所以你只需要这样更新:

localStorage.setItem('token',res.data);