1

I am using a youtube search library

search(searchTerm, opts, function(err, results) {
           if (err) return console.log(err);

           console.dir(results);
      });

and it returns a JS Object with the first video found like this

[ { id: 'xsPHeH-pNQU',
link: 'https://www.youtube.com/watch?v=xsPHeH-pNQU',
kind: 'youtube#video',
publishedAt: '2015-04-10T19:36:16.000Z',
channelId: 'UC9EZGiMrK8-OYbLH3Yfj_QQ',
channelTitle: 'IQ Tests | Personality Tests | Funny Test Videos',
title: '✔ Which Nickname is Perfect For You? (Personality Test)',
description: 'What nickname fits you best? What is your cute nickname? What is your best nickname? What should your nickname be? Be my friend on Facebook ...',
thumbnails: { default: [Object], medium: [Object], high: [Object] } } ]

I am trying to extract the link out of this by using

var videoUrl = results.link; 

or

var videoUrl = results["link"];

but everytime I get it to log to the console what videoUrl is, it keeps saying undefined. The full code looks like this

var searchTerm = message.content.slice(8, 2000);
      search(searchTerm, opts, function(err, results) {
           if (err) return console.log(err);
           var videoUrl = results.link;
           console.dir(results);
           console.log(videoUrl);
           message.channel.send(videoUrl);
      });

Any ideas of what I'm doing wrong?

Jacob Brasil
  • 125
  • 1
  • 6

1 Answers1

0

The object is in an array so you need to reference the item in the array. As it is the first item in the array you need to use [0]

var videoUrl = results[0].link; 
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54