Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate chunks on JSON response

I've got a fairly standard MEAN project setup with the angular-fullstack generator using yeoman.

What I'm finding is that when GETting a largish (over 65536 bytes) json result, it is encoded using gzip and chunked, but the json returned is not valid viewed either in chrome or consumed by my angular client $resource because it contains TWO responses! e.g {name:'hi'}{name:'hi'} for a single id or [{..},{..}][{..},{..}] for a array.

The server api endpoint was autogenerated from the angular-fullstack generator and looks something like:

// Get list of worlds
exports.index = function(req, res) {
  World.find(function (err, worlds) {
    if(err) { return handleError(res, err); }
    res.json(200, worlds);
  });
};

If i slice the data so it's not chunked, then the json is well formed. I've checked the mongo db and the data is ok there too and debugging the worlds variable, I can JSON.stringify and get the expected string result without any duplicates. but the moment it's sent, I'm getting a doubling up of json result on the response.

Update for comment

angular-fullstack 2.0.4

the schema looks like:

'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var WorldSchema = new Schema({
  name: String,
  info: String,
  active: Boolean,
  tiles: [Schema.Types.Mixed]
});

module.exports = mongoose.model('World', WorldSchema);

seeded with:

 var newWorld = new WorldModel({
                    _id: planet._objectId,
                    name: "SimDD World",
                    tiles : seed()
                });
                newWorld.save();

...

var seed = function () {
    var data = [];
    for (var i = 0; i < planet.HEIGHT; i++) {
        for (var j = 0; j < planet.WIDTH; j++) {
            data.push({
                coords:{
                    x:i,
                    y:j
                },
                type:'.'
            });
        }
    }
    return data;
}
like image 951
Joe Avatar asked Jul 19 '14 02:07

Joe


1 Answers

// app.use(require('connect-livereload')());

I came across the same problem when building my angular-fullstack app (thanks, DaftMonk), after some extensive debugging using node-inspector, turns out the JSON data gets passed to the livereload module and gets duplicated when it comes out. Disabling this middleware eliminated the problem for me.

like image 145
Mrvicadai Avatar answered Oct 04 '22 14:10

Mrvicadai