2015-01-30 15 views
6

Tôi nhận được lỗi sau khi thực thi tập lệnh node.js, tôi đã cố gắng điều tra rất nhiều bằng cách thêm console.log() để theo dõi lỗi nhưng không tìm được giải pháp nào. [Lưu ý: Tôi cũng đã tìm kiếm giải pháp Stackoverflow khác, nhưng không ai trong số đó đã giúp]Node.js undefined: 1 [SyntaxError: Kết thúc đầu vào không mong đợi]

undefined:1 
    {"ydht":{"status":{"code":200,"message":"OK"},"records":[ 
                ^
SyntaxError: Unexpected end of input 
at Object.parse (native) 
at IncomingMessage.<anonymous> (/tmp/subs_20140130/inc/getData.js:36:24) 
at IncomingMessage.EventEmitter.emit (events.js:95:17) 
at IncomingMessage.<anonymous> (_stream_readable.js:745:14) 
at IncomingMessage.EventEmitter.emit (events.js:92:17) 
at emitReadable_ (_stream_readable.js:407:10) 
at emitReadable (_stream_readable.js:403:5) 
at readableAddChunk (_stream_readable.js:165:9) 
at IncomingMessage.Readable.push (_stream_readable.js:127:10) 
at HTTPParser.parserOnBody [as onBody] (http.js:142:22) 

Đây là mã của tôi:

var options = { 
    host: '<my host>', 
    port: 3128, 
    path: 'http://<some host>:4080'+searchQuery, 
    method: 'GET', 
    headers: { 
    'App-Auth': cert 
    } 
}; 
var req = http.request(options, function(res) { 
    res.setEncoding('utf8'); //DEBUG 
    for (var k in options) { console.log("[LOGGING] options :" + k + " = " + options[k]);} //DEBUG 
    res.on('data', function (resData) { 
    var resObj = ""; 
    resObj = JSON.parse(resData); 
    console.log("[LOGGING] Response:: "+resObj);    
    if(resObj.ydht.status.code === 200 && resObj.ydht.records[0].key.length > 0) { 
     console.log("[LOGGING] Email "+em+" Key  "+resObj.ydht.records[0].key);   
     var filePath = basePath + '/setData'; 
     var setd = require(filePath); 
     setd.setMagData(resObj.ydht.records[0].key, ycacert, is_sub); 
    } else { 
     console.log("[LOGGING] Fail to fetch data em  "+em+" nl  "+nl); 
    } 
    }); 
    res.on('end', function() { 
    console.log("[LOGGING] connection closed"); 
    }); 
}); 
req.on('error', function(err) { 
    console.log("[LOGGING] Fail to fetch data em  "+em+" nl  "+nl); 
}); 
req.end(); 

Khi tôi gọi là api sử dụng lệnh curl, tôi nhận được json dưới đây hợp lệ phản hồi:

{"ydht":{"status":{"code":200,"message":"OK"},"records":[{"metadata":{"seq_id":"intusnw1-14B3579A577-3","modtime":1422531339,"disk_size":99},"key":"[email protected]","fields":{"em":{"value":"[email protected]"},"is_confirm":{"value":""},"nl":{"value":"offerpop1"}}}],"continuation":{"scan_completed":false,"scan_status":200,"uri_path":"/YDHTWebService/V1/ordered_scan/dts.subs_email?order=asc&start_key=a0"}}} 

Trả lời

16

Gọi lại data được gọi nhiều lần với các đoạn phản hồi. Trên mỗi cuộc gọi lại, bạn cần phải thêm phản hồi vào chuỗi và sau đó trên end, đó là khi bạn phân tích cú pháp.

var req = http.request(options, function(res) { 
    res.setEncoding('utf8'); 
    var body = ""; 
    res.on('data', function(resData) { 
     body += resData; 
    }); 
    res.on('end', function() { 
     var json = JSON.parse(body); 
     if (json.ydht.status.code === 200 && json.ydht.records[0].key.length > 0) { 
      var filePath = basePath + '/setData'; 
      var setd = require(filePath); 
      setd.setMagData(json.ydht.records[0].key, ycacert, is_sub); 
     } else { 
      console.log("[LOGGING] Fail to fetch data em  " + em + " nl  " + nl); 
     } 
    }); 
}); 
+0

Hãy để tôi thử giải pháp của bạn –

+0

Cảm ơn bạn rất nhiều. Giải pháp của bạn đã cứu sống tôi: D –

+0

Cảm ơn bạn rất nhiều nó đã làm việc !!! –

0

Trước hết Cảm ơn bạn Ben vì đã phân tích nguyên nhân chính xác. Tôi đã thử giải pháp Ben đề xuất, nhưng vì dữ liệu phản hồi của tôi quá lớn nên nó bắt đầu cho tôi lỗi "treo máy". Vì vậy, tôi phải thiết kế lại các giải pháp sử dụng Node.js yêu cầu mô-đun

//Load the request module (Dont forget to include it in package.json dependency "request": "2.x.x") 
var request = require('request'); 

request('http://xys.com/api', function (error, response, body) { 
    //Check for error 
    if(error){ 
     return console.log('Error:', error); 
    } 

    //Check for right status code 
    if(response.statusCode !== 200){ 
     return console.log('Invalid Status Code Returned:', response.statusCode); 
    } 

    console.log(body); // Here is the response body 

}); 
0

Đối với tôi khi tôi nhận được lỗi này:

undefined:1 
[ 

Nó bởi vì các tập tin .json được lưu lại dưới dạng:

8 -bit unicode BOM, Win (CRLF) thay vì: 8-bit unicode, Win (CRLF)

nó cần phải là sau này cho tôi!

LATE

Các vấn đề liên quan