TypeError: Converting circular structure to JSON in nodejs

JavascriptJsonnode.js

Javascript Problem Overview


I am using request package for node.js

Code :

 var formData = ({first_name:firstname,last_name:lastname,user_name:username, email:email,password:password});

  request.post({url:'http://localhost:8081/register', JSON: formData}, function(err, connection, body) {

exports.Register = function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
	console.log("Request data " +JSON.stringify(req));

Here I am getting this error :

> TypeError: Converting circular structure to JSON

Can anybody tell me what is the problem

Javascript Solutions


Solution 1 - Javascript

JSON doesn't accept circular objects - objects which reference themselves. JSON.stringify() will throw an error if it comes across one of these.

The request (req) object is circular by nature - Node does that.

In this case, because you just need to log it to the console, you can use the console's native stringifying and avoid using JSON:

console.log("Request data:");
console.log(req);

Solution 2 - Javascript

I also ran into this issue. It was because I forgot to await for a promise.

Solution 3 - Javascript

Try using this npm package. This helped me decoding the res structure from my node while using passport-azure-ad for integrating login using Microsoft account

https://www.npmjs.com/package/circular-json

You can stringify your circular structure by doing:

const str = CircularJSON.stringify(obj);

then you can convert it onto JSON using JSON parser

JSON.parse(str)

Solution 4 - Javascript

use this https://www.npmjs.com/package/json-stringify-safe

var stringify = require('json-stringify-safe');
var circularObj = {};
circularObj.circularRef = circularObj;
circularObj.list = [ circularObj, circularObj ];
console.log(stringify(circularObj, null, 2));


stringify(obj, serializer, indent, decycler)

Solution 5 - Javascript

I was able to get the values using this method, found at careerkarma.com

Output looks like this. Preview of the output

I just run this code in the debugger console. Pass your object to this function.
Copy paste the function also.

 const replacerFunc = () => {
    const visited = new WeakSet();
    return (key, value) => {
      if (typeof value === "object" && value !== null) {
        if (visited.has(value)) {
          return;
        }
        visited.add(value);
      }
      return value;
    };
  };
  
  JSON.stringify(circObj, replacerFunc());

Solution 6 - Javascript

It's because you don't an async response For example:

app.get(`${api}/users`, async (req, res) => {
    const users = await User.find()
    res.send(users);
   })

Solution 7 - Javascript

I forgotten to use await keyword in async function. with the given systax

blogRouter.put('/:id', async (request, response) => {
  const updatedBlog = Blog.findByIdAndUpdate(
    request.params.id,
    request.body,
    { new: true }
  );
  response.status(201).json(updatedBlog);
});

Blog.findByIdAndUpdate should be used with the await keyword.

Solution 8 - Javascript

If you are sending reponse , Just use await before response

await res.json({data: req.data});

Solution 9 - Javascript

This is because JavaScript structures that include circular references can't be serialized with a"plain" JSON.stringify.

https://www.npmjs.com/package/circular-json mentioned by @Dinesh is a good solution. But this npm package has been deprecated.

So use https://www.npmjs.com/package/flatted npm package directly from the creator of CircularJSON.

Simple usage. In your case, code as follows

import package

// ESM
import {parse, stringify} from 'flatted';

// CJS
const {parse, stringify} = require('flatted');

and

console.log("Request data " + stringify(req));

Solution 10 - Javascript

Came across this issue in my Node Api call when I missed to use await keyword in a async method in front of call returning Promise. I solved it by adding await keyword.

Solution 11 - Javascript

I came across this issue when not using async/await on a asynchronous function (api call). Hence adding them / using the promise handlers properly cleared the error.

Solution 12 - Javascript

I was also getting the same error, in my case it was just because of not using await with Users.findById() which returns promise, so response.status().send()/response.send() was getting called before promise is settled (fulfilled or rejected)

Code Snippet

app.get(`${ROUTES.USERS}/:id`, async (request, response) => {
	const _id = request.params.id;
	try {
        // was getting error when not used await
		const user = await User.findById(_id);
		if (!user) {
			response.status(HTTP_STATUS_CODES.NOT_FOUND).send('no user found');
		} else {
			response.send(user);
		}
	} catch (e) {
		response
			.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR)
			.send('Something went wrong, try again after some time.');
	}
});

Solution 13 - Javascript

I had a similar issue:-
const SampleFunction = async (resp,action) => {
try{
if(resp?.length > 0) {
let tempPolicy = JSON.parse(JSON.stringify(resp[0]));
do something
}
}catch(error){
console.error("consoleLogs.Utilities.XXX.YYY", error);
throw error;
}
.
.
I put await before JSON.parse(JSON.stringify(resp[0])).
This was required in my case as otherwise object was read only.
Both Object.create(resp[0]) and {...resp[0]} didn't suffice my need.

Solution 14 - Javascript

Try this as well

console.log(JSON.parse(JSON.stringify(req.body)));

Solution 15 - Javascript

TypeError: Converting circular structure to JSON in nodejs: This error can be seen on Arangodb when using it with Node.js, because storage is missing in your database. If the archive is created under your database, check in the Aurangobi web interface.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHitu BansalView Question on Stackoverflow
Solution 1 - JavascriptScimonsterView Answer on Stackoverflow
Solution 2 - JavascriptSatya PrakashView Answer on Stackoverflow
Solution 3 - JavascriptDinesh NadimpalliView Answer on Stackoverflow
Solution 4 - JavascriptMohit GuptaView Answer on Stackoverflow
Solution 5 - JavascriptArun Prasad E SView Answer on Stackoverflow
Solution 6 - JavascriptBaye Fall In TIC SAFView Answer on Stackoverflow
Solution 7 - JavascriptSaurav guptaView Answer on Stackoverflow
Solution 8 - JavascriptNeeraj NegiView Answer on Stackoverflow
Solution 9 - JavascriptPankaj ShindeView Answer on Stackoverflow
Solution 10 - JavascriptJatin PatelView Answer on Stackoverflow
Solution 11 - JavascriptSasi Kumar MView Answer on Stackoverflow
Solution 12 - JavascriptWasitShafiView Answer on Stackoverflow
Solution 13 - JavascriptMayank SinghView Answer on Stackoverflow
Solution 14 - JavascriptSharanya WarrierView Answer on Stackoverflow
Solution 15 - JavascriptRavi JainView Answer on Stackoverflow