0
我在写一个简单的程序,它向reddit发出一个请求,并以JSON格式返回页面。我通过将'.json'附加到reddit网址的末尾来完成此操作。例如,如果我想让我的个人资料页面,我会做“www.reddit.com/user/stebon24.json”。在node.js中使用JSON.parse时出错
这是我的程序到目前为止。我将记录并描述下面的错误。
var http = require('http');
var options = {
host: 'www.reddit.com',
path: ''
};
module.exports = function(username) {
console.log(username);
options.path = '/user/' + username + '.json';
var userData;
http.get(options, function(res) {
res.on('data', function(data) {
userData += data;
console.log(userData);
});
res.on('end', function() {
userData = JSON.parse(userData);
console.log(userData);
});
});
};
至于错误,当它发生的程序获取到它需要运行JSON.parse()来点。我知道这一点,因为我可以看到它从“数据”事件记录结果时输出原始JSON。然后这个错误是输出...
undefined:1
undefined{"kind": "Listing", "data": {"modhash": "", "children": [{"kind": "t1
^
SyntaxError: Unexpected token u
at Object.parse (native)
at IncomingMessage.module.exports (/home/stephen/Desktop/karmacrawler/engine/crawlUser.js:23:20)
at IncomingMessage.EventEmitter.emit (events.js:126:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1367:20)
at TCP.onread (net.js:403:27)