我在查找JSON响应内的属性时遇到问题。我有这个:
{
"global": {
"loading": false,
"error": false,
"currentUser": false,
"userData": {
"repositories": false
}
},
"language": {
"locale": "en"
},
"login": {
"login": {
}
},
"router": {
"location": {
"pathname": "/login",
"search": "",
"hash": ""
},
"action": "POP"
}
}
在渲染时,我要检查该属性是否存在
render() {
let isSuccess;
const respuesta = this.props.response;
console.info(respuesta.has('login'));
if (respuesta.login.has('response')) {
isSuccess = this.props.response.login.response.success;
if (isSuccess) {
localStorage.removeItem('token');
localStorage.setItem('token', this.props.response.login.response.token);
}
}
那个console.info(respuesta.has('login'));返回true,但在下一个错误消息中返回:“ TypeError:无法读取属性'has'未定义”
我遵循了以下教程:
https://medium.freecodecamp.org/login-using-react-redux-redux-saga-86b26c8180e