angularjs - 抛出新的 ERR_INVALID_ARG_TYPE ('chunk' , ['string' ,'Buffer' ],chunk);TypeError[ERR_INVALID_ARG_TYPE] :The "chunk" arg must be type string or Buffer

标签 angularjs json node.js

我正在尝试使用 Node js 服务将 .json 文件的内容获取到 angularjs 方法中。但是我得到以下错误:

_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)

下面是对应的代码片段...

angular controller:注释行是我尝试过但失败的所有内容。

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 服务:

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:注释掉的行是我尝试过但失败的行。此外, Node 服务代码片段中的注释行没有给出错误,并且在记录时正确显示,但 Controller 响应时的数据为空白。

最佳答案

我在这里有点猜测,但我认为您只需要在您的 Node 代码中将 res.end() 更改为 res.send()。当您流式传输数据 block 时使用“end”方法,然后在您完成所有操作后调用 end()。 “发送”方法用于一次发送响应并让 Node 处理流。

此外,请确保您发回了一个字符串!

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

关于angularjs - 抛出新的 ERR_INVALID_ARG_TYPE ('chunk' , ['string' ,'Buffer' ],chunk);TypeError[ERR_INVALID_ARG_TYPE] :The "chunk" arg must be type string or Buffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51929027/

相关文章:

node.js - 如何在 sequelize 中使用 Multipolygon 数据类型创建表字段

javascript - 如何更改 Electron 应用程序的图标

Node.js 将 HTML 作为响应发送给客户端

javascript - 解析 Html 中的 promise 值 -> Ctrl -> Factory -> 延迟对象上的资源链

javascript - 在指令中自定义模板

json - 使用json解码转换接口(interface)

php - file_get_contents() -> SSL 操作失败,代码为 1

angularjs - 错误 : Property 'then' does not exist on type 'Hero[]'

html - Angular-bootstrap 日历 mwl -> 事件的其他符号而不是点

java - 根据 JSON 模式验证 JSON(在 Java 中)