SyntaxError: Unexpected token <
AndrusAsumets opened this issue · 10 comments
Hello,
I've been getting this error for a couple of times among thousands of searches:
SyntaxError: Unexpected token <
at Object.parse (native)
at Request._callback (/home/colours/Dropbox/colours/code/13/worker/node_modules/youtube-node/lib/youtube.js:91:33)
at Request.self.callback (/home/colours/Dropbox/colours/code/13/worker/node_modules/youtube-node/node_modules/request/request.js:123:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request.<anonymous> (/home/colours/Dropbox/colours/code/13/worker/node_modules/youtube-node/node_modules/request/request.js:877:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage.<anonymous> (/home/colours/Dropbox/colours/code/13/worker/node_modules/youtube-node/node_modules/request/request.js:828:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
line 91 corresponds to:
var data = JSON.parse(body);
So I assume if there's something wrong with the response from YouTube, it will crash during parsing. I'm not an expert or anything, but shouldn't this line be surrounded with try/catch?
Cheers,
Andrus Asumets
How many requests is doing the script you have?
Fixed: 8c6454e
Thank you for the fix, I'll try it out soon.
My web app, which currently is in an early alpha stage, has about 10 active users/testers. The search queries the youtube-node every time a key is pressed, so there's a flux of data moving in and out every once in a while.
I started using the newest version of the youtube-node and ran into a new issue when doing multiple requests:
url output:
https://www.googleapis.com/youtube/v3/search?key={key}&part=snippet&q=a&maxResults=2
https://www.googleapis.com/youtube/v3/search?key={key}&part=snippet%2Csnippet&q=ab&maxResults=2
https://www.googleapis.com/youtube/v3/search?key={key}&part=snippet%2Csnippet%2Csnippet&q=abc&maxResults=2
https://www.googleapis.com/youtube/v3/search?key={key}&part=snippet%2Csnippet%2Csnippet%2Csnippet&q=abcd&maxResults=2
which can be fixed by adding
self.parts = [];
to youtube.js line 127
what is the code you're using? I think I need a method of data cleansing or perhaps you can assist with the code to see if the problem is to instantiate the module.
The code is pretty bad, but I hope it could be helpful:
socket.on('search_youtube', function (data) {
var channel_name = data['hash'];
var search_string = data['search_string'];
youtube.search(search_string, 10, function(result_data) {
try {
var result_array = [];
var id_list = '';
for (var i = 0; i < result_data['items'].length; i++) {
var id = result_data['items'][i]['id']['videoId'];
if(i == 0) { id_list = id_list + id };
if (i > 0) { id_list = id_list + ',' + id };
}
youtube.getById(id_list, function(result_data2) {
for (var i = 0; i < result_data2.length; i++) {
var result_object = {};
result_object['id'] = result_data2[i]['id'];
result_object['title'] = result_data2[i]['snippet']['title'];
result_object['thumbnail_url'] = result_data2[i]['snippet']['thumbnails']['default'];
result_object['duration'] = convert_time(result_data2[i]['contentDetails']['duration']);
result_array.push(result_object);
if (i == result_data2.length - 1) {
socket.emit('youtube_results', { result_data: result_array });
}
}
});
}
catch (err) { console.log(err) }
});
});
I also had to modify
callback( response.items[0] )
on line 115 inside getById function at youtube.js to
callback( response.items );
so that way I would send out a string of videoId's and receive info about every videoId during a single request.
Anyhow, thanks for "wasting" so much time on this :)
- Update youtube-node to 1.0.1 (5406240)
- Use this code: https://gist.github.com/paulomcnally/9314167
Added youre convert_time function and call socket.emit('youtube_results', { result_data: result_array });
Regards
Hey.
Thanks for the gist, the code looks so much more elegant than mine. I tried using it outside socketio using a loop with 10 different characters to search the youtube and it worked fine. As soon as I put it inside socketio it started failing after the first query. I have now modified the youtube.js so it would clear parts and parameters before making another call. It now takes one additional attribute, which is youtube's api key. Also, I've added a related function for getting related videos. Here's the gist:
You can do pull request? You know how?
Okay, I think I succeeded in making a pull request for the first time. First I created a fork, then cloned, then made changes to youtube.js, then commited the changes, then pushed to github, and then created a pull request. The pull request is now displaying on "paulomcnally / youtube-node".