-1

I can see the stringifyed Object proberties in console log but I am getting error while printing them. The following console log works fine, and I can see all properties in console log, but if I print metadata.Age then my app crashes. My object properties looks as following in console log. {"Full_name":"John Abraham","Age":"26","Location":"Antwerpen"}

var metadata;
metadata = data["https://shakir01.net/user_metadata"];
console.log(JSON.stringify(metadata));
console.log(JSON.stringify(metadata.Age)); //Crashes
qiAlex
  • 4,290
  • 2
  • 19
  • 35
Muj
  • 132
  • 3
  • 14
  • can you provide an error that browser throws? – qiAlex Nov 02 '18 at 01:08
  • it says Age is undefined – Muj Nov 02 '18 at 01:10
  • I guess that not. `console.log(JSON.stringify(undefined)) // undefined` it can't crash. More likely it throws `Uncaught TypeError: Cannot read property 'Age' of undefined`, am I right? This means that `metadata` is not defined and you are trying to do `JSON.stringify(undefined.Age)` and it throws an error – qiAlex Nov 02 '18 at 01:19
  • {"Full_name":"John Abraham","Age":"26","Location":"Antwerpen"} this is what I get from first console log so how can metadata be undefined ? And yes I get the exact same error. – Muj Nov 02 '18 at 01:23

1 Answers1

0

In my case, My object was undefined on first twee attempts that's why I was getting undefined error. On 3rd attempt I was getting my full object, but meta.Age would't let my app to get to that point. Tried this

var Age;
if (typeof metadata != "undefined") {
      Age = metadata.Age;
}
Muj
  • 132
  • 3
  • 14