Been trying to solve this problem for a while now. I'm aware there are similar problems on stack overflow, but none of the solutions are working for me and I need some help. I'm getting the error message
undefined:1
SyntaxError: Unexpected end of JSON input
at Object.parse (native)
at IncomingMessage.<anonymous> (B:\-\memeFinder.js:27:20)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
When running this code:
var http = require('http');
getRedditMemes();
function getRedditMemes () {
var options = {
host: 'www.reddit.com'
path: '/r/me_irl/hot/.json?count=20'
headers: {'User-agent':'xxxxx', 'Accept-Charset':'UTF-8'}
};
http.get(options, function (response) {
var str = '';
response.on('data', function (chunk) {
console.log(chunk)
str += chunk;
})
response.on('end', function () {
console.log(str);
var root = JSON.parse(str); //This line is where I'm getting the error
})
}).end();
}
I'm aware that there are other answers here that explain that the error is a result of incomplete json, but most of the applicable answers say to place the parsing code in response.on('end') which I'm already doing. It's probably some stupid small thing, but help is much appreciated, thanks.
Edit: Thanks for the responses everyone. I appreciate the quick help.
Networkpanel and confirmed your http call returns data?