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

user824624

i am working to save a html snippet into the mongodb database, the save operation is successful however retrieving the data from mongo causes a failure 'TypeError: Converting circular structure to JSON'.

the code is pretty simple

let database;
let connection;

exports.connectDatabase = function(MONGO_URL,DB_NAME) {
  MongoClient.connect(  MONGO_URL,  { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
      database = client.db(DB_NAME);
      connection = client;
    }
  );
};

exports.getDatabase = function(){
  return database;
};



exports.saveHtml = async function(req, res) {
  try{
    await getDatabase().collection('htmls').insertOne({name:req.body.title,  html:req.body.content });
    res.send('success');
  }catch(e){
    console.log(e);
    res.json(404);
    return;
  }
}

exports.getSavedHtmls =  async function(req,res){
  try{
    let html = await getDatabase().collection('htmls').find({});
    res.send(html);
  }catch(e){
    console.log(e);
    res.json(404);
    return;
  }
}

the error is

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'NativeTopology'
    |     property 's' -> object with constructor 'Object'
    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'
    --- property 'topology' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/Users/xisizhe/Documents/server/node_modules/[email protected]@express/lib/response.js:1123:12)

How would I solve the issue?

Joe

getDatabase().collection('htmls').find({}) returns a FindCursor.

You will need to call toArray to get an array of documents that can be converted to JSON.

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

How do I solve "TypeError: Converting circular structure to JSON" in NodeJS

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

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)

TypeError: Converting circular structure to JSON when getting data from mysql DB

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

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