Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Converting circular structure to JSON #4311

Closed
BumpyBill opened this issue Jun 10, 2020 · 5 comments
Closed

TypeError: Converting circular structure to JSON #4311

BumpyBill opened this issue Jun 10, 2020 · 5 comments
Labels

Comments

@BumpyBill
Copy link

BumpyBill commented Jun 10, 2020

Im trying to send the request data, but I get an error message. Any idea on how I can fix this? (This error only occurs when I send JSON)

Here is my server.js:

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

const app = express();


app.use(cors());

const routes = { post: require("./routes/post") };
app.use("/post", routes.post);

const PORT = process.env.PORT || 8000;


app.listen(PORT, (err) => {
  if (err) console.log(err);
  else console.log(`Listening On Port ${PORT}`);
});

And here is my routes/post.js (where the main issue is I believe):

const router = require("express").Router();

router.post("/example", (req, res) => {
  res.json(req);
});

module.exports = router;

Edit: Here's the error message:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at C:\Users\624234\Documents\ExpressTemplate\server\routes\post.js:4:17
    at Layer.handle [as handle_request] (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\624234\Documents\ExpressTemplate\node_modules\express\lib\router\index.js:174:3)
@BumpyBill
Copy link
Author

I think the "req" object was too long, but I won't close it because I'm probably incorrect

@ghinks
Copy link

ghinks commented Jun 10, 2020

Yes there is a circular dependency between req and res and this is not a bug. Try sending particular props from the req object back in the response.

this is from the docs

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

@davidmashe
Copy link

Not a bug. @ghinks gave a good example, circular objects being non-serializable is expected behavior. Closing issue.

@gzwei1
Copy link

gzwei1 commented Dec 28, 2020

non-serializable

Hi @davidmashe , does circular in 'circular objects' mean recursive?

@rofrol
Copy link

rofrol commented Oct 23, 2021

This worked for me:

https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format/21875464#21875464

I have just copied decycle function from https://github.com/douglascrockford/JSON-js/blob/master/cycle.js and then:

import { writeFileSync } from 'fs';
// ...
writeFileSync('res.json', JSON.stringify(decycle(res, undefined)));

instead of import you can use const fs = require('fs');;

https://stackoverflow.com/questions/43622337/using-import-fs-from-fs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants