0

TypeError: Converting circular structure to JSON at JSON.stringify ()

var userInfo = currentUser.children;
      if(currentUser.type ==1){
        userInfo.push(currentUser);
        //console.log(typeof userInfo);
      }

It produce me error Converting circular structure to JSON. How can i come over this please guide.

Ricky
  • 35
  • 5
  • can you please post your values ? that you are pushing – Muhammad Usman Mar 20 '18 at 10:28
  • A circular structure in a JSON means you have strored a Selfreferencing variable inside the Object or just a Selfexecuting functions like a timeout or interval – Tobias Fuchs Mar 20 '18 at 10:29
  • Read [this](https://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json) – standby954 Mar 20 '18 at 10:30
  • {user_id: 1, social_id: null, type: 1, name: "xys", …}, {user_id: 2, social_id: null, type: 1, name: "xyz", …} ..so ..on – Ricky Mar 20 '18 at 10:32

2 Answers2

0

Your problem is due to something like this:

var oscar = {
  name: 'oscar'
}

var john = {
  name: 'john',
  friend: oscar
};

oscar.friend = john;

console.log(JSON.stringify(oscar));

JSON.stringify navigates the tree defined by the object until it finishes. The problem here is that oscar has a property that references john, and then john references oscar, so the function would enter an infinite loop:

{"name": "oscar", "friend": {"name": "john", "friend": {"name": "oscar", ...

And so on. Always avoid circular references when converting to JSON.

Oscar Paz
  • 18,084
  • 3
  • 27
  • 42
0

You can't push "currentUser" to "userInfo", because userInfo itself is a property of current user, you are trying to reference an object to itself. Just push the data you want to push into an array.

var userInfo = [];
if(currentUser.type ==1){
  userInfo.push(currentUser);
  //console.log(typeof userInfo);
}
Stephan T.
  • 5,843
  • 3
  • 20
  • 42