0

I am trying to retrieve one particular value from within a two-levels deep object data structure. First off, though, I am saving into a variable within the function, like this:

getTargetId() {
    if (this.authenticationService.isAuthenticated()) {
        const userInfo = sessionStorage.getItem('currentUser');
        console.log(userInfo);
    }
}

From:

console.log(userInfo);

I get this back in the console:

{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}

What I want to do is specifically pull out the "_id" value here.

I tried:

console.log(userInfo.data._id);

But then my IDE is showing me an error:

'Property '_id' does not exist on type 'string'.

How do I dig out "_id" in this case?

Rey
  • 1,393
  • 1
  • 15
  • 24

4 Answers4

3

You are accessing it wrong
Try userInfo.data._id
In the log of your object you can see by the {} notation that data is another object, so after accessing data you can access its properties just as you would with any other object.

I also see that you are getting

'Property '_id' does not exist on type 'string'.

This could mean that you never parsed the information. To find out if this is the case this should be right:

Running->

console.log(userInfo);

Returns->

{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}

Just after this code:
Running->

console.log(typeof userInfo);

Returns->

"string"

With your edits, I can see that this is the case.
Try:

userInfo = JSON.parse(sessionStorage.getItem('currentUser') );
console.log(userInfo.data._id);
Mark E
  • 3,403
  • 2
  • 22
  • 36
  • This stil gets me this in my IDE: 'Property 'data' does not exist on type 'string'. – Rey May 22 '17 at 22:13
  • @Ademo then it seems like you need to `JSON.parse` that string. – jonrsharpe May 22 '17 at 22:17
  • The I guess you would need to post more code. If you ran the code `console.log(userInfo);` and got the object data, you should be able to run this `console.log(userInfo.data._id);` and get the id. If it doesn't work, you are changing the variable somewhere else – Mark E May 22 '17 at 22:17
  • Yeah, @jonrsharpe could be right, may be you are trying to access the info before parsing it – Mark E May 22 '17 at 22:18
  • Yup. I had just come to the same conclusion and had fixed it after reading the comment above from @jonsharpe. Will mark yours as correct rather than my own answer. – Rey May 22 '17 at 22:35
1

The _id property is under the data key:

const response = {
    "token":"sometoken.value",
    "data": {
       "_id":"8cd0362c0",
       "phone":"555-4343"
     }
};
console.log(response.data._id)

You can also use destructuring:

const { _id } = response.data;
console.log(_id)

or:

const { data: { _id }} = response;
console.log(_id);
Rob M.
  • 35,491
  • 6
  • 51
  • 50
1

So, as @jonsharpe pointed out, the key was to JSON.parse the string first. So this gets me the value I need for "_id":

getTargetId() {
    if (this.authenticationService.isAuthenticated()) {
        const userInfo = JSON.parse(sessionStorage.getItem('currentUser'));
        console.log(userInfo.data._id);
    }
}
Rey
  • 1,393
  • 1
  • 15
  • 24
0

Actually your string is returned as JSON string. So you have to parse it into object using JSON.parse() if you are using js or with $.parseJSON() if you are using Jquery. So your updated code now looks like this.

var user ='{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"}}';
var k = JSON.parse(user);
alert(k.data._id);

And Fiddle is here. Thank You

breakit
  • 356
  • 2
  • 8