I am using the angular-fullstack generator for my project. This is really handy because it helps scaffolding out your API.
I have come across a problem when testing my API. If my API returns only a few results everything seems fine, but if the results are significant in size eg. over 100, the return is duplicated. Here is one of my query methods.
Flight.find({pilot: req.params.pilot}, function (err, flight) {
console.log("flight: ", flight.length); // returns correct number of results
if(err) { return handleError(res, err); }
if(!flight) { return res.send(404); }
return res.json(flight);
});
The returned results when only 3 for example are like so:
[
{
"_id": "552edd6cfa67a54c302bf9b6",
"pilot": "Austin",
"__v": 0
},
{
"_id": "552edd6cfa67a54c302bf9b7",
"pilot": "Austin",
"__v": 0
},
{
"_id": "552edd6cfa67a54c302bf9b8",
"pilot": "Austin",
"__v": 0
}
]
But a much larger dataset is always duplicated eg:
[
{
// imagine 100+ objects
}
]
[
{
// imagine the exact same 100+ objects - with same ids as above
}
]
Why does this happen? When I check the total results directly from MongoDB terminal it has the correct amount of results, the same is in the console log in my query method above, but the result in the browser is duplicated on large sets.
UPDATE:
Also I've found out that if I limit the fields that are returned, I don't get duplicate results. There are about 10 fields in every item, if I only show the name for example it doesn't duplicate, if I add about 3 fields it does duplicate all the results again.