Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw new ERR_INVALID_ARG_TYPE('chunk',['string','Buffer'],chunk);TypeError[ERR_INVALID_ARG_TYPE]:The "chunk" arg must be type string or Buffer

I am trying to get the contents of a .json file using a node js service into an angularjs method. But am getting following error:

_http_outgoing.js:700 throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk); ^ TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object at ServerResponse.end (_http_outgoing.js:700:13)

here are the corresponding code fragments...

angular controller: the commented lines are all of those which i have tried and failed with.

var currentProcess = "process_1cA";
$scope.storestats = [];
var resAss = $resource('/procs/getstorestats');
var stats = resAss.get({
  process: currentProcess,
  date: date.getFullYear() + "" + m + "" + d
});
stats.$promise.then(function(response) {
  if (response != undefined) {
    //    var r = JSON.parse(response);
    //$scope.storestats.push(r);
    //$scope.storestats.push(r);

    //var r = JSON.parse(response);
    $scope.storestats.push(response);
    //angular.forEach(r, function(value, key) {
    //    $scope.storestats.push({key : value});
    //});
  }
});

NODEJs service:

httpApp.get('/procs/getstorestats', function(req, res, next) {

try {
    fs.readFile(cfg.routestatspath + "storestats-"+req.query.process + "-" + req.query.date + ".json", function (err, data) {
        var msgs1 = JSON.parse(data);
        //var r  = data.toString('utf8');
        var msgs2 = JSON.stringify(msgs1);
        console.log(msgs1);
        res.end(msgs1);
    });
}
catch (err) {
    res.end(err.toString());
}});

P.S: The commented out lines are those which i have tried out with and failed. Also, the commented lines in the node service code snippet, give no error, and when logged show it correctly, but the data when in response of the controllers is blank.

like image 692
Harshith Rai Avatar asked Aug 20 '18 10:08

Harshith Rai


1 Answers

I'm guessing a bit here, but I think you just need to change res.end() to res.send() in your Node code. The "end" method is used when you are streaming chunks of data and then you call end() when you're all done. The "send" method is for sending a response in one go and letting Node handle the streaming.

Also, be sure you are sending a string back!

httpApp.get('/procs/getstorestats', function(req, res, next) {

  try {
    fs.readFile(cfg.routestatspath + "storestats-"+req.query.process + "-" + req.query.date + ".json", function (err, data) {
        var msgs1 = JSON.parse(data);
        //var r  = data.toString('utf8');
        var msgs2 = JSON.stringify(msgs1);
        console.log(msgs1);
        res.send(msgs2);  // NOTE THE CHANGE to `msg2` (the string version)
    });
  }
  catch (err) {
    res.send(err.toString());  // NOTE THE CHANGE
  }
});
like image 98
Jordan Kasper Avatar answered Oct 20 '22 01:10

Jordan Kasper