0

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.

Neil
  • 7,861
  • 4
  • 53
  • 74

1 Answers1

0

OK I've also found another SO answer that kind of fixes it but it's not clear if this is really a bug:

app.use(compression());

Removing this line from express - \openshift\server\config\express.js if using angular-fullstack will not duplicate the results in the browser.

I've tested the API using curl and the Postman app in Chrome. These two tests don't return duplicates.

The bug seems to be in the browsers interpretation of the data. I am happy to continue using compression() in express after seeing these independent results.

Neil
  • 7,861
  • 4
  • 53
  • 74