Dominic Scanlan
Dominic Scanlan

Reputation: 1009

Querying Mongodb Subdocuments error Converting circular structure to JSON

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);
});

Upvotes: 2

Views: 15980

Answers (3)

U.A
U.A

Reputation: 3373

Or try this

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

Upvotes: 5

Amey
Amey

Reputation: 480

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!

Upvotes: 25

Kevin Brady
Kevin Brady

Reputation: 1724

try this

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

Upvotes: 10

Related Questions