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
}
};