2

I created a RESTful API using node.js, express.js, and mongodb. I started creating my routes by pulling documents from a MongoDB collection which work perfectly.

Example Collection Document

{ 
"_id" : ObjectId("51ace8c04cc8ea865df0923e"), 
"title" : "Some Example Title", 
"producer" : 
 { 
  "company" : "Your Company Name"
 } 
}

Works - and it also works if I do a .find({query}) instead of a generic find()

   app.get('/something', something.findAll);
    exports.findAll = function(req, res) {
        db.collection('something', function(err, collection) {
            collection.find().toArray(function(err, items) {
                res.contentType('json');
                res.header("Access-Control-Allow-Origin", "*");
                res.header('Access-Control-Allow-Methods', 'GET, PUT');
                res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
                res.send(items);
            });
        });
    };

But when I try to call a embedded document (i.e. subdocument) it using dot.notation it breaks.

*Doesn't Work*

db.something.find( { 'producer.company': 'ABC123' } )

Or even if I try

db.something.find( {producer: {company: 'ABC123'} } );

I get an error message saying.

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at ServerResponse.res.json (../lib/response.js:185:19)
    at ServerResponse.res.send (..//lib/response.js:117:21)
    at ../routes/recipes.js:81:8
    at Collection.find (../node_modules/mongodb/lib/mongodb/collection.js:931:5)
    at ../routes/recipes.js:73:14
    at Db.collection (../lib/mongodb/db.js:462:44)
    at exports.findByCat (../routes/recipes.js:72:5)
    at callbacks (../node_modules/express/lib/router/index.js:161:37)
    at param (../node_modules/express/lib/router/index.js:135:11)

Can anybody help me find a workaround, or let me know if there are any errors with my approach.

Thanks!

2
  • Can you describe what's in the collection? Otherwise, I can't give you any more information that you can get from that error message. Commented Jun 4, 2013 at 19:52
  • That suggests that the json serializer is running into a circular argument Commented Aug 12, 2013 at 10:30

1 Answer 1

1

You've basically done this

> var a = {a:1}
undefined
> a.b = a
{ a: 1, b: [Circular] }
> JSON.stringify(a)
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at repl:1:7
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)

Somewhere in your code you are creating a self-referential object

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.