Querying Mongodb Subdocuments error Converting circular structure to JSON

11,134

Solution 1

try this

router.get('/findByContactName/:surname', function(req, res){
Lead.find({"contacts.surname":req.params.name}).exec(function(err, leads){
res.send(leads);
});

Solution 2

TL;DR:

Change var leads = Lead.find({"contacts.surname":req.params.name});

To:

var leads = await Lead.find({"contacts.surname":req.params.name});

Explanation

model.find() just returns a query. It does not execute the query for you. So, 'leads' variable is currently a Mongoose query document.

When you do res.send(leads), express internally does this:

JSON.stringify(leads)

stringify() cannot convert circular structures to JSON. Hence, the error.

If someone here could shed light on why Mongoose query document is a circular structure, that would be great!

Solution 3

Or try this

 router.get('/findByContactName/:surname', async (req, res)=> {
      const leads = await Lead.find({"contacts.surname": req.params.surname });
      res.send(leads);
    });
Share:
11,134
Dominic Scanlan
Author by

Dominic Scanlan

Updated on June 05, 2022

Comments

  • Dominic Scanlan
    Dominic Scanlan almost 2 years

    I have a document with an array of subdocuments:

    {
      "company": "test plc",
      "address": [
        {
          "addr1": "37",
          "addr2": "",
          "addr3": "test",
          "addr4": "",
          "addrcity": "",
          "addrcounty": "test",
          "addrpostcode": "test"
        },
        {
          "addr1": "37",
          "addr2": "",
          "addr3": "test",
          "addr4": "",
          "addrcity": "",
          "addrcounty": "test",
          "addrpostcode": "test"
        },
        {
          "addr1": "37",
          "addr2": "",
          "addr3": "test",
          "addr4": "",
          "addrcity": "",
          "addrcounty": "test",
          "addrpostcode": "test"
        }
      ],
      "contacts": [
        {
          "name": "test",
          "surname": "testing",
          "title": "master"
        },
        {
          "name": "test",
          "surname": "testing",
          "title": "master"
        }
      ]
    }
    

    What I would like to do is return a list of documents by searching the contacts.surname property.

    var leads = Lead.find({"contact.surname":req.params.name});
    

    This causes an error "Converting circular structure to JSON" but I am not sure why.

    added on edit:

    This is my collection schema:

    var leadsSchema = new Schema({
      company: String,
      address:
      [
        {
          addr1: String,
          addr2: String,
          addr3: String,
          addr4: String,
          addrcity: String,
          addrcounty: String,
          addrpostcode: String
        }
      ],
      contacts:
      [
        {
          name: String,
          surname: String,
          title: String
        }
      ]
    });
    var Lead = mongoose.model('leads', leadsSchema);
    

    Here are my two routers:

    This returns all from the collection find:

    router.get('/', function(req, res) {
      Lead.find({}).exec(function(err, leads) {
        res.send(leads);
      });
    });
    

    This causes the circular error:

    router.get('/findByContactName/:surname', function(req, res) {
      var leads = Lead.find({"contacts.surname":req.params.name});
      res.send(leads);
    });
    
  • Dominic Scanlan
    Dominic Scanlan about 9 years
    Ah good spot. I think it was a typo though when adding this question. Updated to: var leads = Lead.find({"contacts.surname":req.params.name}); res.send(leads); but still same error
  • Kevin Brady
    Kevin Brady about 9 years
    Check out this SO article to try and track down what field is causing the circulat structure. (is req.params.name just a string value?) stackoverflow.com/questions/7005205/…
  • Dominic Scanlan
    Dominic Scanlan about 9 years
    yes. I've even tried Lead.find({"contact.surname":"testing"});
  • Kevin Brady
    Kevin Brady about 9 years
    does the query work from the mongo shell? If so, then you can rule out that the query or the DB is the issue. can you provide more of the code surrounding the DB call you are making?
  • Dominic Scanlan
    Dominic Scanlan about 9 years
    thanks for the suggestion. Yes the shell returns the row i was expecting.
  • Kevin Brady
    Kevin Brady about 9 years
    can you provide more code before and after the DB call so to see if we can identify where the circular structure error is coming from
  • Jason
    Jason about 2 years
    This should be higher up! The mongoose model relies on asynchronous functions, so we would need to await them before carrying on.