TypeError: Converting circular structure to JSON -- Next.js

Blake Lucey

I'm writing an API in Next.js to insert data into a MySQL database. The error I'm experiencing relates to a circular reference, but I'm having trouble finding it. Please be aware that I am also using Axios to write this API.

I'm running into the following error:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Query'
    |     property '_timer' -> object with constructor 'Timer'
    --- property '_object' closes the circle

Here is my API:

import type { NextApiRequest, NextApiResponse } from "next";
import * as pool from "../../../src/utils/dbConnection";
import console from "console";

export default async (req: NextApiRequest, res: NextApiResponse) => {
  const { email, password, firstname, lastname } = req.body as any;
  let conn;
  try {
    conn = await pool.getConnection();
    const rows = await conn.query(
      `INSERT INTO Users(email, password, firstname, lastname) VALUES(?, ?, ?, ?)`,
      [email, password, firstname, lastname]
    );

    res.status(200).json(JSON.stringify(rows));
  } catch (err) {
    console.log(err);
  } finally {
    if (conn) conn.end(); // close connection
  }
};
pzaenger

I guess your problem is here: res.status(200).json(JSON.stringify(rows));. So, from the docs of res.json:

Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().

Therefore you just need res.json(rows); or maybe res.status(201).json(rows);, where 201 means Created.

Edit:

I think there might be another problem. The query to insert data does not return the inserted rows (if you use mysql). So, please log rows to check its content - and as mentioned by @jfriend00 there could be and properly are some circular references.

However, you can get the id of the inserted row or the number of affected rows for example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Chrome sendrequest error: TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in nodejs

TypeError: Converting circular structure to JSON when trying to POST request

VM1374:32 Uncaught TypeError: Converting circular structure to JSON(…)

Doing a proper MongoDB Query (TypeError: Converting circular structure to JSON)

TypeError converting circular structure to json at JSON.stringify (<anonymous>)

TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'

Angular - TypeError: Converting circular structure to JSON

Firestore references create a "TypeError: Converting circular structure to JSON"

Express Middleware: ERROR: TypeError: Converting circular structure to JSON

Elliptic : TypeError : Converting circular structure to JSON

[Vue warn]: Error in render: "TypeError: Converting circular structure to JSON

ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FormGroup'

TypeError: Converting circular structure to JSON property 'name' closes the circle

Moving code to an exported function results in TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON with StorybookJS on React Native

avoiding TypeError: Converting circular structure to JSON

Angular JS $scope.$apply giving interpolation error "TypeError: Converting circular structure to JSON"

TypeError: Converting circular structure to JSON - find error in json

Amazon Cognito confirmPassword fails with (TypeError: Converting circular structure to JSON)

GraphCool TypeError: Converting circular structure to JSON

how to solve (Uncaught TypeError: Converting circular structure to JSON)

Uncaught TypeError: Converting circular structure to JSON after Angular / dependencies update

TypeError: Converting circular structure to JSON in retrieving html string from mongodb

Sequelize: TypeError: Converting circular structure to JSON

Reading fixture is throwing error TypeError: Converting circular structure to JSON

"TypeError: Converting circular structure to JSON" with GET request

"TypeError: Converting circular structure to JSON" with GET request

Axios TypeError: Converting circular structure to JSON