-1

I have this object:

var body = {
  "LocationInfo": {
    "Stores": [
      {
        "Number": 524,
        "Name": "Store"
      }
    ]
  }
};

But I can't read anything inside the object, for example:

console.log(body["LocationInfo"]["Stores"][0]); is undefined

This is so basic... I'm not sure what's wrong.

JJJ
  • 32,902
  • 20
  • 89
  • 102
alyx
  • 2,593
  • 6
  • 39
  • 64
  • It works on my console. `Object {Number: 524, Name: "Store"}` is logged. – fuyushimoya Aug 21 '15 at 18:35
  • Same here. ```var x = body["LocationInfo"]["Stores"][0];```. x is not undefined for me – kmc059000 Aug 21 '15 at 18:36
  • You will have to show more of your code. The code you've posted works fine. https://jsfiddle.net/3d7ou2hy/ – JJJ Aug 21 '15 at 18:36
  • it is working for me – ismnoiet Aug 21 '15 at 18:36
  • @Juhana this is the full JSON i'm trying to read: http://pastebin.com/1T8HZ5SJ – alyx Aug 21 '15 at 18:43
  • That object doesn't have a `LocationInfo` member. https://jsfiddle.net/3d7ou2hy/1/ – JJJ Aug 21 '15 at 18:45
  • @Juhana it's this property `PersonalLocationInfo` i just shortened it – alyx Aug 21 '15 at 18:47
  • Then it works equally well. https://jsfiddle.net/3d7ou2hy/2/ Seriously, you have to show a **complete** example that **replicates** the problem – edit the jsfiddle so that it matches the problem you're describing. It's impossible to help if you don't show enough code. – JJJ Aug 21 '15 at 19:01

3 Answers3

0

It does work as expected; undefined is just the return value of console.log.

Source: https://stackoverflow.com/a/11109316/1760344

Community
  • 1
  • 1
digitalsteez
  • 345
  • 1
  • 2
  • 14
  • Why is this downvoted? `console.log("digitalsteez") VM332:2 digitalsteez undefined` This is the same thing that occurs when you run jrbaldwinn's code in the console: `console.log(body["LocationInfo"]["Stores"][0]); VM343:2 Object {Number: 524, Name: "Store"} undefined` There's even a symbol that implies that it is the function's return value. – digitalsteez Aug 21 '15 at 19:31
  • If you are indeed running this on Node, I'm not sure what's going on. This is what I get with your code on Node: `$ node test.js { Number: 524, Name: 'Store' }` – digitalsteez Aug 21 '15 at 19:35
0

There is nothing wrong with your object. The reason you see that "undefined" is not because your object is wrong or something in it is undefined. Its just that the console appends undefined after console.log(); function is called. For more knowledge, refer this:

Chrome/Firefox console.log always appends a line saying 'undefined'

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Prashant_M
  • 2,868
  • 1
  • 31
  • 24
0

Ok I solved my issue: there's a bug in the node request module that wraps double escapes, so I did:

body = JSON.parse(body);
body = JSON.parse(body);

twice in a row and it worked!

alyx
  • 2,593
  • 6
  • 39
  • 64