0

I am assigning the soundcloud Playlists tracks to the variable "audios".But when i am trying to access the individual tracks objects form audios it says undefined.

<script src="https://connect.soundcloud.com/sdk/sdk-3.3.2.js"></script>
<script>
        SC.initialize({
          client_id: 'e0daf19e8a6cb0b30ab12bf9ea4df192'
        });

        var i=348525692;
        var all_tracks=[];
        var audios =new Array();

        //invoking playlist no: 348525692
        audios = SC.get('/playlists/'+i).then(function(playlist) {
            return playlist.tracks;
        }
        );

        // Receiving the tracks of the playlist invoked
        console.log(audios);



</script> 

Which produces below output.

enter image description here

and when i use audios.length or audios.id etc. it says undefined. I want to access this data inorder to proceed ahead with my tasks. Please help.

enter image description here

kiranvj
  • 32,342
  • 7
  • 71
  • 76

2 Answers2

2

You are getting it as undefined because your value is getting logged before the array is populated with its values. The SC.get().then() looks like a promise based API call to me but you console log it outside the then. You have to use the array after the values are populated in it from your API call value assignment.

    <script src="https://connect.soundcloud.com/sdk/sdk-3.3.2.js"></script>
        <script>
            SC.initialize({
            client_id: 'e0daf19e8a6cb0b30ab12bf9ea4df192'
            });

            var i=348525692;
            var all_tracks=[];
            var audios =new Array();

            //invoking playlist no: 348525692
            SC.get('/playlists/'+i).then(function(playlist) {
                    audios = playlist.tracks;
                    console.log(audios);
                    console.log(audios[0]); //individual track
                    console.log(audios.length);
                    console.log(audios[0]['id']); //individual track id, can also be written as audios[0].id
                }
            );
    </script> 
  • you are absolutely correct. this way it was happening for me too. But this way i cant return the variable outside. and my purpose is to return the result outside and then use it in another function to process it. – Prashant Yadav Jun 09 '20 at 11:36
  • you can use this outside as well...you can call the other function inside the `then` after `audios = playlist.tracks;`. You can pass `audios` as an argument to that function and it will be able to read the values from params and use it for processing. – Simple_Programmer Jun 09 '20 at 11:45
0

the result you get is an Object. length property is defined on arrays and string or if you do a custom definition. regarding the id property, its not defined inside the object on the first level, but its defined inside the _results array. my guess is that you want to access the _results array and get it's length and ids.

audios._result.forEach(audio => {
  console.log(audio.id);
});
console.log('audios length:' + audios._result.length);
vlad katz
  • 534
  • 3
  • 9