How to access JSON Object.$$state.value?

All we need is an easy explanation of the problem, so here it is.

Warning: I’m going to sound like I have no idea what I’m talking about here, because I kind of don’t. I’m in the process of self-learning Javascript and AngularJS through a lot of trial and error coding.

I have some javascript code (hesitant to copy here because it’s a mess) that returns an Object with the following structure:
enter image description here

What I want to save to a variable is the object corresponding to Object.$$state.value in the picture. This object has username, hash and salt, which are what I care about. I don’t know what all the other stuff like $$state are or how they got there.

However, if I do this (let’s call the main Object “whatIHave”):

var whatIWant = whatIHave.$$state.value;

this does not work. whatIWant is null.

Does anyone recognize what’s going on here? What is $$state, how did it get there, and how can I extract the value I want?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

So that is a promise. You need to do something like:

whatIHave.then(function(whatIWant) {
  // Work here
});

I highly recommend you to research what a promise is (like this link)

If you’re curious enough, about what is that $$state and what is that value, I will explain a bit:

The promises have a $$state and there angular saves all the callback functions you want to call in a pending array (all those function you registered with .then like I explained before).

It also have the status: resolved (1) and rejected (2)

Finally, when you resolve or reject the promise, the value you pass when doing that, is saved in value.

You’re trying to cheat in here, because when you try to access that value, it could be not there yet (that is what async is all about).

So the idea is learning the basic of promises, learning how to work with them and then use your whatIHave correctly.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply