24

I have some code that pulls all documents from a collection and puts it onto a webpage. a simplified version looks like this:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('systemMonitor', mongoServer),
    db;

var app = new express();

app.get('/drives', function(req, res) {
  db.collection('driveInfo', function(err, collection) {
    if (err) throw err;
    collection.find({}, function(err, documents) {
      res.send(documents);
    });
  });
});

dbConnector.open(function(err, opendb) {
  if (err) throw err;
  db = opendb;
  app.listen(80);
});

I have a driveInfo collection which contains a long list of documents. Each document contains nested objects. What I would like to do, is whenever someone visits /drives in their browser, to print the entire collection as a json object so that I can grab everything with jquery later (beginnings of an api)

However, I get an error saying "TypeError: Converting circular structure to JSON". The error on the page points to this line of code:

collection.find({}, function(err, documents) {
  res.send(documents);
});

I'm unsure what the problem is, or where the self-reference is. Am I not querying the collection properly?

1
  • 1
    Have you tried logging the output of the query?
    – ZeMoon
    Feb 25, 2015 at 9:58

5 Answers 5

26

Not sure what version of the API you are using, but i think that your syntax might be wrong looking at the API spec:

http://docs.mongodb.org/manual/reference/method/db.collection.find/

This is the declaration:

db.collection.find(<criteria>, <projection>)

And you are definitely misusing the projection parameter. Passing a callback like you are doing seems to return the db object in the result, which is causing the circular error during JSON serialization in express.

The correct code for the find all operation should be something like:

collection.find({}).toArray(function(error, documents) {
    if (err) throw error;

    res.send(documents);
});
2
  • 1
    ah yes, I pulled the original code from another stackoverflow answer which I guess was incorrect syntactically. The one you provided works though, thanks!
    – Simon
    Feb 25, 2015 at 16:39
  • You are welcome ;).. this kind of stuff happens of often with Node.js since the API's are not stable. Feb 25, 2015 at 16:48
6

In my case I was getting the error because I was querying(using mongoose find method) without doing an await. Please see below

Query that gave the error (as I haven't executed this query using await) :

const tours = Tour.find({
    startLocation: {
      $geoWithin: { $centerSphere: [[longitude, latitude], radius] }
    }
  });

Error that I got on postman due to this :

"message": "Converting circular structure to JSON\n    --> starting at object with constructor 'NativeTopology'\n    |     property 's' -> object with constructor 'Object'\n    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'\n    --- property 'topology' closes the circle"

How I got rid of the above error (added await) :

 const tours = await Tour.find({
        startLocation: {
          $geoWithin: { $centerSphere: [[longitude, latitude], radius] }
        }
      });
0

callback option is from Mongoose not from MongoDB see docs.

// Mongoose Docs : callback option
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});


// Example
app.get( '/api/users' , (req,res,done)=>{
  let getUsers = NewUser.find({},(err,data)=>{
    if(err) return done(err);
    res.json(data)
  });
});

Look that the response is into callback that in your case it would be

YourModel.find({}, function(err, documents) {
  if(err) return done(err);
  res.send(documents);  //  <-- here
});
// <-- not here

In Mongo there is a cursor method to access the documents next() see docs :

var myCursor = db.bios.find( );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
    var myName = myDocument.name;
    print (tojson(myName));
}

You can find CRUD operations in mongo docs at manual/crud. In Query Documents you will see db.inventory.find( {} ) : To select all documents in the collection, pass an empty document as the query filter parameter to the find method.


Async/Await function solution : Mongo Docs

app.get( '/api/users' , async (req,res)=>{
  const getUsers = await NewUser.find({});
  res.json( getUsers );
})

< callback > solution : Mongoose Docs.

app.get( '/api/users' , (req,res,done)=>{
  let getUsers = NewUser.find({},(err,data)=>{
    if(err) return done(err);
    res.json(data)
  });
});

0

const res1 = await db.collection("some-db").find()

Here, res1 will contain a "cursor" which has a circular structure, hence the given error is thrown.

Try adding const res2 = await res1.toArray() to the code.

Here, res2 will now contain an array of documents, pointed by cursor res1, which is the documents you were querying for.

0

In my case, I forgot to await the .find() method and was sending an unresolved promise back in the response.

Changed this:

const posts = Post.find({});

to this:

const posts = await Post.find({});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.