1

Here is the code:

var holder;
var findSong;
var embedHolder;


$(".hypemList ol").on('click','li',function (){

   holder = $(this).text();
   findSong = SC.get('/tracks', { q: holder });
   embedHolder = findSong._result[0].uri;
   SC.oEmbed(embedHolder // song to embed
      , { color: "ff0066"
      , auto_play: false
      , maxwidth: 500
      , maxheight: 1000
      , show_comments: true  // options
    , element: document.getElementById("soundTest") // what element to attach player to
  });

});

What I would like to happen:

I have a list of songs that I would like to play. When I click on the song, I will grab that string and then search soundcloud API using that string. I will take index 0 of the result and place that in SC.oEmbed to play the song.

When I open dev console, I get this error when I click on a song --

Uncaught TypeError: Cannot read property '_result' of undefined.

I believe I need to wait for findSong to finish and then I can use embedHolder and place that into SC.oEmbed.

What I have done:

I found this: returning array from function in javascript. I tried following what they did, but it didn't seem to work.

I'd appreciate it if someone could point me in the right direction on what to do next.

Community
  • 1
  • 1
M K
  • 11
  • 2

1 Answers1

0

SC.get() has 3 parameters: the path, the options and the callback. The callback is a function that will be called once SC.get is done. The callback in this case takes the array of results as a parameter. Using the callback in your code will look like this:

 SC.get('/tracks', { q: holder }, function(tracks){ 
        embedHolder = tracks[0].uri;
        SC.oEmbed(embedHolder // song to embed
          , { color: "ff0066"
          , auto_play: false
          , maxwidth: 500
          , maxheight: 1000
          , show_comments: true  // options
        , element: document.getElementById("soundTest") // what element to attach     player to
      });

});

Hope it helps!

Hartger
  • 329
  • 1
  • 14