抛出新的TypeError('请求路径包含非转义字符')

时间:2018-01-26 01:44:15

标签: node.js typeerror

当我尝试运行节点js应用程序时出现此错误:

  

_http_client.js:127         抛出新的TypeError('请求路径包含非转义字符');         ^

     

TypeError:请求路径包含未转义的字符       在新的ClientRequest(_http_client.js:127:13)       在Object.request(http.js:38:10)       at Object.exports.getDuration(/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:53:20)       at /Users/paulcarron/git/integration-test-runner/modules/jobRunningService.js:75:21       在IncomingMessage。 (/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:35:13)       在emitNone(events.js:111:20)       在IncomingMessage.emit(events.js:208:7)       at endReadableNT(_stream_readable.js:1056:12)       at _combinedTickCallback(internal / process / next_tick.js:138:11)       at process._tickCallback(internal / process / next_tick.js:180:9)

经过一些调查后我得出结论是因为getEventLog.js中的+ myId(参见问题最底部的代码)。整个脚本由此函数在另一个脚本中调用:

child.stderr.on('data', function(uuid, data) {

  getEventLog.getId(uuid, function(err, id){
    if(err) return console.log(err)
    getEventLog.getDuration(id, function(err, duration){
        if(err) return console.log(err)
        jobLogger.info(escape(data) + "<br />");
    })
  });

});

我认为myId中可能有空格,所以我添加了console.log("myId: " + myId)并发现它返回myId: [object Object]。如何解决此问题,以便myId是一个格式正确的ID?

var http = require("http");
var fs = require('fs');
var async = require('async');
var readline = require('readline')
//var db = require('./dbPool');

//get file name
var options =  {
    "method" : "GET",
    "hostname" : "127.0.0.1",
    "port" : "18080",
    "path" : "/api/v1/applications/"
};

exports.getId = function (uuid, callback) {
    var req = http.request(options, function (res) {

        var chunks = [];

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function () {
            var body = JSON.parse(Buffer.concat(chunks));

            var arrFound = Object.keys(body).filter(function(key) {
                if (body[key].name.indexOf(uuid) > -1) { 
                    return body[key].name;
                }
            }).reduce(function(obj, key){
                obj = body[key].id;
                return obj;
            }, {});
            callback(null, arrFound);
        });
    });
    req.end();
}

exports.getDuration = function (myId, callback) {
    //getEventLog.getId(function(err, id){

    //get file name
    var options =  {
        "method" : "GET",
        "hostname" : "127.0.0.1",
        "port" : "18080",
        "path" : "/api/v1/applications/" + myId
    };

    var req = http.request(options, function (res) {
        console.log(options)

        var chunks = [];

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function () {
            var body = JSON.parse(Buffer.concat(chunks));

            var attempts = body.attempts

            var arrFound = Object.keys(body).filter(function(key) {

                return attempts[0].duration;

            }).reduce(function(obj, key){
                obj = body.attempts[0].duration;
                return obj;
            }, {});
            //console.log(arrFound);
            callback(null, arrFound);
        });
    });
    req.end();
 //   })
};

1 个答案:

答案 0 :(得分:1)

这里的问题是myId是一个JSON对象而不是一个字符串。这意味着返回了一个html页面,而不是我需要的JSON位置。