首页 > 解决方案 > 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

标签: javascriptreactjshasownproperty

解决方案


推荐阅读