javascript - React hasOwnProperty 找不到属性
问题描述
我在查找我知道在 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')); 返回真,但如果它告诉我一个错误,则在下一个:“TypeError:无法读取未定义的属性'has'”
我按照以下教程进行操作:
https://medium.freecodecamp.org/login-using-react-redux-redux-saga-86b26c8180e
解决方案
推荐阅读
- loops - SAS:在宏程序的循环中使用动态宏变量
- ios - 如何基于 MKAnnotation 引脚执行 segue?
- r - 无法从 github 安装软件包 - 无法解释的错误消息
- laravel-nova - Laravel Nova - 如何在索引上显示部分文本区域文本
- acumatica - 将主要列表添加到站点地图
- php - jQuery 到 PHP 提交和发布不返回任何结果
- android - 将数据从 Fragment 中的 RecyclerView 传递到另一个 Fragment
- uwp-xaml - XLS0413 在类型“TreeView”中找不到属性“ItemsSource”
- c# - 从 xlsx 文件读取/写入 CustomDocumentProperties 而无需互操作
- javascript - 为什么承诺写成“then(..).catch(..)”?