代码之家  ›  专栏  ›  技术社区  ›  Harshith Rai

throw new ERR_INVALID_ARG_TYPE('chunk',['string','Buffer'],chunk);TYPE ERR or[ERR_INVALID_ARG_TYPE]:chunk参数必须是string或Buffer类型

  •  1
  • Harshith Rai  · 技术社区  · 7 年前

    我正试图获取 .json 使用node js服务将文件转换为angularjs方法。但是我正在被跟踪 错误:

    _http_传出.js:700 抛出新的ERR_INVALID_ARG_TYPE('chunk',['string','Buffer'],chunk); ^ 位于ServerResponse.end(_http_outgoing.js:700:13)

    下面是相应的代码片段。。。

    角度控制器:

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

    注释掉的行是我尝试过但失败的行。此外,节点服务代码段中的注释行不会给出错误,并且在记录时会正确显示,但在响应控制器时的数据为空。

    2 回复  |  直到 7 年前
        1
  •  6
  •   Jordan Kasper    7 年前

    我在想,但我觉得你需要改变 res.end() res.send() end() 当你都做完了。“send”方法用于一次性发送响应并让节点处理流。

    另外,一定要发回一根绳子!

    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
      }
    });
    
        2
  •  2
  •   user6184932 user6184932    6 年前

    我也有类似的错误。因为我正在将process.pid传递给res.end()。当我将process.pid改为string时,它起作用了

    res.end(process.pid.toString());
    
        3
  •  0
  •   Harshith Rai    7 年前

    控制器:

                    var resAss = $resource('/procs/getstorestats');
                    var stats =  resAss.query({process: currentProcess, date: date.getFullYear() + "" + m + "" + d});
                    stats.$promise.then(function (response) {
                        $scope.storestats.push(response);
                    }
    

    节点服务:

    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 msgs2 = JSON.stringify(msgs1);
            console.log(msgs2);
            res.end(msgs2);
        });
    }
    
        4
  •  0
  •   chenchunaidu    5 年前

    var options = {
        uri: 'https://api.github.com/user/repos',
        qs: {
            access_token: 'xxxxx xxxxx' 
        },
        headers: {
            'User-Agent': 'Request-Promise'
        },
        json: true // Automatically parses the JSON string in the response
    };
    
    rp(options)
        .then(function (repos) {
    
        })
        .catch(function (err) {
    
        });
    
        5
  •  0
  •   Yanov    5 年前

    感谢用户6184932,它起作用了

    try {
        await insertNewDocument(fileNameDB, taskId);
        res.end(process.pid.toString());
    } catch (error) {
        console.log("error ocurred", error);
        res.send({
            "code": 400,
            "failed": "error ocurred"
        })
    }