Tuesday, May 30, 2017

Uncaught TypeError: Converting circular structure to JSON


Hi Guys
You will see this error on converting a json to string using JSON.stringify .
Full stacktrace is like
Uncaught TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at <anonymous>:1:6
Below is the sample code that can cause this exception. You can see that we have created an element object in line number 1 and in line number 2 we are assigning the same element object to the node attribute of the element. This is having a circular reference . Hence you will experience the exception.
var element={"node":{}};
element.node=element;
JSON.stringify(element);
You might also face the error for the following block of code :
var element={"node":{}};
element.node=element;
angular.toJson(element);
Uncaught is added since the exception is not catched by a catch block . According to MSDN, A TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function .So here it is explicitly thrown by stringify method.It is also possible that a common utility method is throwing this exception. Check if your code has the following statement .
new TypeError("Converting circular structure to JSON");

No comments:

Post a Comment

ec2-user@ec2 Permission denied