question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgres TypeError: Converting circular structure to JSON

See original GitHub issue

Issue Description

So I am using postgresql with typeorm and nestjs and I have a user entity. When I run .findOne({username }) it works fine, but doing .find({}) to get all simply gives this error:

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 SelectQueryBuilder.<anonymous> (C:\Users\thebo\Documents\ProxyByrd\packages\backend\node_modules\.pnpm\typeorm@0.2.28\node_modules\typeorm\query-builder\SelectQueryBuilder.js:1553:67)

Expected Behavior

The following code should return the list of users to the backend:

    async findAll(): Promise<User[]> {
        return await this.usersRepository.find({ take: 5 });
    }

Actual Behavior

It errors with circular socket structure error:

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 SelectQueryBuilder.<anonymous> (C:\Users\thebo\Documents\ProxyByrd\packages\backend\node_modules\.pnpm\typeorm@0.2.28\node_modules\typeorm\query-builder\SelectQueryBuilder.js:1553:67)

Steps to Reproduce

  1. Use nestjs and typeorm
  2. Try to call .find({}) on an injected repository in a service.

My Environment

Dependency Version
Operating System Windows 10
Node.js version 14.0.0
Typescript version 4.0.5
TypeORM version 0.2.28

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • [x ] postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Cleatuercommented, Nov 18, 2020

@ErolPeel When saving a One-to-Many relation in TypeORM you need to be careful to only reference one side of the relation. For example when you want to save a PartyEntity with a PartyContactEntity, do it like this:

const partyContactEntity: PartyContactEntity = new PartyContactEntity()
partyContactEntity.contactMedium = savedPostalContactEntity.contactMedium
// important: do not set the partyContactEntity.party here as this will result in the 
// "TypeError: Converting circular structure to JSON" error when saving the contact with the party

const partyEntity: PartyEntity = new PartyEntity( ... )
partyEntity.contacts = [partyContactEntity]

manager.save(partyEntity)
1reaction
ghostcommented, Nov 17, 2020

It happens in TypeORM 0.2.22 and 0.2.18 as well. I guess, there is some modelling error on my side?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Converting Circular structure to JSON #1523 - GitHub
The TypeError: Converting circular structure to JSON shows up when you trying to JSON.stringify something which has a reference to itsself, ...
Read more >
Converting circular structure to JSON at Object.stringify (native ...
I have a basic table that one my postgresql db. ... Unhandled rejection TypeError: Converting circular structure to JSON at Object.stringify ...
Read more >
What is TypeError: Converting circular structure to JSON?
TypeError : Converting circular structure to JSON occurs when you try to reference your variable name within the JSON object.
Read more >
Converting circular structure to JSON in nodejs-node.js
TypeError : Converting circular structure to JSON in nodejs: This error can be seen on Arangodb when using it with Node.js, because storage...
Read more >
TypeError: Converting circular structure to JSON and ...
You must select a tag to post in this category. Please find the tag relating to the section of the course you are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found