realisation
realisation

Reputation: 638

JSON.stringify prints object to console, object.property prints undefined

My angular app is giving me a problem where

console.log(JSON.stringify($rootScope.user));

is printing out the user object like expected, but...

When I try

console.log($rootScope.user.objectId) 

or

var id = $rootScope.user.objectId;
console.log(id);

I'm getting undefined. What's up with that? userId is definitely one of the properties printed out in the JSON.stringify, so... I'm confused.

{"username":"thinmint","email":"[email protected]","objectId":"FmjGa7POFH",
 "createdAt":"2015-05-28T18:39:08.362Z","updatedAt":"2015-05-28T18:39:08.362Z"}

Thats an example value of $rootScope.user.

Upvotes: 1

Views: 990

Answers (1)

Sreejith
Sreejith

Reputation: 166

   var json = {
            "username": "thinmint", "email": "[email protected]", "objectId": "FmjGa7POFH",
            "createdAt": "2015-05-28T18:39:08.362Z", "updatedAt": "2015-05-28T18:39:08.362Z"
        };
        var json1 = JSON.parse(JSON.stringify(json));
        alert(json1.username);

Upvotes: 2

Related Questions