mongodb nodejs - converting circular structure

后端 未结 1 1473
眼角桃花
眼角桃花 2020-12-10 03:05

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         


        
相关标签:
1条回答
  • 2020-12-10 03:31

    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);
    });
    
    0 讨论(0)
提交回复
热议问题