2

I have this code right here that I want to parse data from, it supposed to get the url that's located in the url: part, the code is :

var search = require('youtube-search');

var opts = {
  maxResults: 3,
  startIndex: 1
};

search('deadmau5', opts, function(err, results) {
  if(err) return console.log(err);
     var title1 = results.indexOf("url:");
     var title2 = results.indexOf("&feature");
     var titl = results.substr(title1 + 7);
     var result = titl.substr(0, titl.indexOf("&feature"));
  console.log(result);
});

However I get this error:

    var titl = results.substr(title1 + 7);
                        ^
TypeError: Object [object Object],[object Object],[object Object],[object Object
],[object Object],[object Object],[object Object],[object Object],[object Object
],[object Object] has no method 'substr'
    at c:\Users\karim_000\desktop\iplogger.js:12:25
    at c:\Users\karim_000\desktop\node_modules\youtube-search\index.js:42:9
    at Parser.<anonymous> (c:\Users\karim_000\desktop\node_modules\youtube-searc
h\node_modules\xml2js\lib\xml2js.js:255:20)
    at Parser.emit (events.js:95:17)
    at Object.saxParser.onclosetag (c:\Users\karim_000\desktop\node_modules\yout
ube-search\node_modules\xml2js\lib\xml2js.js:225:24)
    at emit (c:\Users\karim_000\desktop\node_modules\youtube-search\node_modules
\xml2js\node_modules\sax\lib\sax.js:615:33)
    at emitNode (c:\Users\karim_000\desktop\node_modules\youtube-search\node_mod
ules\xml2js\node_modules\sax\lib\sax.js:620:3)
    at closeTag (c:\Users\karim_000\desktop\node_modules\youtube-search\node_mod
ules\xml2js\node_modules\sax\lib\sax.js:861:5)
    at Object.write (c:\Users\karim_000\desktop\node_modules\youtube-search\node
_modules\xml2js\node_modules\sax\lib\sax.js:1293:29)
    at Parser.exports.Parser.Parser.parseString (c:\Users\karim_000\desktop\node
_modules\youtube-search\node_modules\xml2js\lib\xml2js.js:273:29)
    at Parser.parseString (c:\Users\karim_000\desktop\node_modules\youtube-searc
h\node_modules\xml2js\lib\xml2js.js:6:61)

I don't exactly know what I'm doing wrong, any insight or help? Thanks in advance

4
  • Are you just trying to get the first result's information? Commented Nov 22, 2014 at 23:51
  • Honestly, I was gonna figure out some way to individually parse all 3 to get them, but if you know of a better way, please tell me! Commented Nov 22, 2014 at 23:55
  • All three? You're getting 10 search results. You just want a string within url: 'http://www.youtube.com/watch?v=h7ArUgxtlJs&feature=youtube_gdata', like http://www.youtube.com/watch?v=h7ArUgxtlJs? Commented Nov 22, 2014 at 23:57
  • oops, yes all 3, and yes that's what I want to parse out. Commented Nov 23, 2014 at 0:01

3 Answers 3

2

This will log each substring of the URL which you are looking for so that you can further manipulate it.

var search = require('youtube-search');

var opts = {
  maxResults: 3,
  startIndex: 1
};

search('deadmau5', opts, function(err, results) {
  if(err) return console.log(err);
  results.forEach(function(result) {
    console.log(result.url.substring(0, result.url.indexOf('&feature')));
  });
});

This yields:

http://www.youtube.com/watch?v=_9IBbMW2o_o
http://www.youtube.com/watch?v=-O85-OXktNs
http://www.youtube.com/watch?v=kWb-zs1L8Ss
Sign up to request clarification or add additional context in comments.

Comments

2

results is an array of objects!

search('deadmau5', opts, function(err, results) {
  if(err) return console.log(err);
  results.forEach(function (result) {
    console.log(result.url);
  });
});

this code will iterate over your array and log the url-property of each object in your results-array!

and as Seth mentioned, you are getting more than 3 searchresults!

Comments

0

Results is a Json Array and you cann't use string methods on it. Get the element that you want from Json and then use substr or any other method

Ex: first element's title in Json array can be found as var title= results[0]['title'];

I suggest you to read about JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.