Reputation: 1
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.
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.
Upvotes: 0
Views: 696
Reputation: 522
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);
Upvotes: 0
Reputation: 690
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>
Upvotes: 2