1

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.

8
  • can u please show us what u have tried and made the code and whats the purpose of stringify ?. can u please bit clear Commented Jun 24, 2015 at 19:46
  • the purpose is to retrieve data from a database. this is what I have tried. I do not know what to try. Commented Jun 24, 2015 at 19:47
  • 1
    Is objectId a property of $rootScope.user? In your post, you say userId is definitely one of the properties, but your code is referencing objectId, not userId. Commented Jun 24, 2015 at 19:52
  • log $rootscope.user once and see if objectId is there. Commented Jun 24, 2015 at 19:54
  • 2
    Time to show us real data. We're all just guessing at this point. Commented Jun 24, 2015 at 19:59

1 Answer 1

2
   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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.