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