1

I have a problem finding a property that I know is inside the JSON response. I have this:

{
  "global": {
    "loading": false,
    "error": false,
    "currentUser": false,
    "userData": {
      "repositories": false
    }
  },
  "language": {
    "locale": "en"
  },
  "login": {
    "login": {

    }
  },
  "router": {
    "location": {
      "pathname": "/login",
      "search": "",
      "hash": ""
    },
    "action": "POP"
  }
}

On render, I have this to check if the property exists

 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);
  }
}

that console.info(respuesta.has('login')); returns me true but in the next if it says me an error: "TypeError: Cannot read property 'has' of undefined"

I followed the tutorial of:

https://medium.freecodecamp.org/login-using-react-redux-redux-saga-86b26c8180e

  • what does `this.props.response.hasOwnProperty('login')` return? perhaps you need to show how the orphaned object you show in the question is related to `this.props.response` in the first place ... `its inside the array` - there is **no array** - so, you are definitely doing something wrong™ – Jaromanda X Mar 11 '19 at 10:11
  • i checked `Object.prototype.hasOwnProperty.call(this.props.response, "login"))` with hard coded response var and it is returning `true` seems like you are not getting the response props correctly or the structure is different – Shadab Ahmed Mar 11 '19 at 10:14
  • or maybe `login` is inherited? – Thomas Mar 11 '19 at 10:16
  • Works fine in the snippet – adiga Mar 11 '19 at 10:17
  • `and as I see in the console.info` - hmmm, could this be an asynchrony issue ... I wonder if the console log *lies* have claimed another victim – Jaromanda X Mar 11 '19 at 10:19
  • Look at the console here https://jsfiddle.net/wmtecdL0/1/ expand the object `{}` that is output to the console - behold that according to the console, `result` existed before it could – Jaromanda X Mar 11 '19 at 10:28
  • When I do an alert of the props that response has its displays me this: Map { "global": Map { "loading": false, "error": false, "currentUser": false, "userData": Map { "repositories": false } }, "language": Map { "locale": "en" }, "login": [object Object], "router": Map { "location": Map { "pathname": "/login", "search": "", "hash": "", "state": undefined }, "action": "POP" } } to I just JSON.stringify it to see it in a clearer way – Miguel Esmoris Lopez Mar 11 '19 at 10:30
  • Or even if I try this: if (this.props.response.hasOwnProperty('login')) { alert("im in"); } it never goes into the alert , and its supposed to do... – Miguel Esmoris Lopez Mar 11 '19 at 10:34
  • `response` is a string and doesn't have any "own properties". Don't stringify the data before the test. – zzzzBov Mar 11 '19 at 11:55
  • Even if I dont stringify it before the test the result is the same :/ Im getting crazy about this – Miguel Esmoris Lopez Mar 11 '19 at 12:00
  • Instead I tried to check it with .has console.info(respuesta.has('login')); and this one returns true – Miguel Esmoris Lopez Mar 11 '19 at 12:20

0 Answers0