My goal is to extract a specific value from a two-level deep object data structure. To begin, I am storing the data in a variable within a function, like so:
getTargetId() {
if (this.authenticationService.isAuthenticated()) {
const userInfo = sessionStorage.getItem('currentUser');
console.log(userInfo);
}
}
When I use this line of code:
console.log(userInfo);
The console output looks like this:
{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}
Specifically, I want to retrieve the "_id" value from this data.
I attempted to access it using:
console.log(userInfo.data._id);
However, my IDE displays an error message:
'Property '_id' does not exist on type 'string'.
How can I successfully extract the "_id" value in this scenario?