I am having strange issues with getting JSON data from backend to our frontend. As you can see below, using the same data object and logging a parsed stringified object, i get a different boolean result for clien->commonFilters->checkedStatus->Active. If I just log the data, then it will also be false but it should be in fact true. This is happening on some arrays of other items in this large json object but I just show this one since they seem to be the same issue.
Asked
Active
Viewed 733 times
3
-
1I think there is no way that code would have this output. Can you show us a piece of code that reproduces your issue? – Tamas Hegedus Sep 12 '17 at 21:00
-
3Note that `console.log` will log a reference to the object passed to it, not a snapshot of the object at the time of logging. The only way you could get the output you've shown us is if you go on tomodify `filterSettings1` after logging. I would for that reason recommend using [`debugger`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) for debugging – Hamms Sep 12 '17 at 21:06
-
2That's right, there is absolutely no way this can happen **only** with the code you posted. You stringify an Object on the second `var`'s line. Then you parse it at two different places. They become two entirely new Objects. You are for sure modifying one of them (or both) shortly after you log them. And as @Hamms mentioned, the Objects logged are dynamic references. They only become "snapshots" when you open them with that little arrow. Only then, their content is fixed in your console. [More info here](https://stackoverflow.com/a/8249333/1913729) – blex Sep 12 '17 at 21:20
-
Thanks guys. It seems that I'm assigning the data to an object that gets changed from some other thing happening during the same time that this is happening. I will dig further. – Razir66 Sep 12 '17 at 21:31
-
Is there a way to actually log a snapshot of an object instead of the reference? – serge Sep 12 '17 at 21:51
-
1@serge You can get rid of any reference by doing `console.log(JSON.parse(JSON.stringify(myObj)));` – blex Sep 13 '17 at 09:40
1 Answers
0
The variables were getting changed later. I tracked it down, did not realize the variables logged were dynamic references. Thanks Hamms and blex for the helpful info. – Razir66 just now edit

Razir66
- 118
- 6