简体   繁体   中英

Why am i getting the 'Converting circular structure to JSON' error in nodeJS while running myqsl query?

I am doing a mysql query stuff to select the all data in a table (name LOGIN) and

https://localhost:2000/select and the params to be passed in the req.body is the table_name value is login and when i hit the api using postman , it returns an empty set with consolelog says,

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at Object.selecting (/home/andiswamy/Documents/fuDDie/shayam/dbConnection.js:67:23)
    at process._tickCallback (internal/process/next_tick.js:68:7)

My code is :

router.get('/select', async (req, res) =>
{   
    let table_name =  req.headers.table_name;
    try {
        let inserting = await db.selecting(table_name);
        console.log(inserting);

        res.send(stringify(inserting));
        res.end();
    } catch(err) {
        console.log(err);
    }
});

async function selecting(table_name) {
    try {
        let result = await testDB.query(`SELECT * FROM ${table_name}`);
        console.log(typeof result);
        result = JSON.stringify(result);
        return result;
    } catch (e) {
        return e;
    }
}
let room = {
  number: 23
};

let meetup = {
  title: "Conference",
  participants: ["john", "ann"]
};

meetup.place = room;       // meetup references room
room.occupiedBy = meetup; // room references meetup

JSON.stringify(meetup); // Error: Converting circular structure to JSON

Refer to the above example this error occours when you refer both the objects to each other and try to stringify them.

Here is a atricle about Circular reference.

Understanding The Error 'Converting circular structure to JSON'

You could use the module check-circular-reference to check whether exist circular reference and print its attribute chain.

and then delete the circular attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM