Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henryoladj's avatar

Getting and Display Records from API - Any Solution?

I am trying to display list of tracks for each album, it is working in the API but not with VUE Template

<ul class="tracklist">
                <li class='tracklistRow' v-if="album in albums">    
                    <div class='trackCount'>
                        <img class='play' src='/assets/images/icons/play-white.png' onclick='playSong()'>
                        <img class='pause' src='/assets/images/icons/pause.png' style='display:none;' onclick='pauseSong()'>
                        <span class='trackNumber'>{{ album.track.id }}</span>
                    </div>                      
                        <div class='trackInfo'>
                            <span class='trackName'>{{ album.track.track.title }}</span>
                        </div>  
                    <div class='trackOptions'>
                        <img class='optionsButton' src='/assets/images/icons/more.png'>
                    </div>

                    <div class='trackDuration'>
                        <span class='duration'>{{ album.track.track.duration }}</span>
                    </div>
                </li>
                <div class="rLabel" v-if="album">
                    &copy; {{ album.albumDetails.recordlabel }}
                </div>
            </ul>
<script>
    import axios from 'axios';

    export default {
        name: 'album',

        props:['id'],
        data: function(){
            return {
                album: null,
                albums: []
            }
        },
        methods: {
            getData(){
                var that = this
                axios.get('http://api.localhost/api/album/'+this.id)
                    .then(function (response){
                        that.album = response.data.data;
                    })

            }
        },
        mounted: function(){
            this.getData()
        }
    }


</script>

Can someone tell me what i am doing wrong.

0 likes
4 replies
alan9608's avatar

Should the second line be v-for instead of v-if?

henryoladj's avatar

@alan9608 so i did this

<ul class="tracklist">
                <li class='tracklistRow' v-if="track" v-for="track in album.track.track">   
                    <div class='trackCount'>
                        <img class='play' src='/assets/images/icons/play-white.png' onclick='playSong()'>
                        <img class='pause' src='/assets/images/icons/pause.png' style='display:none;' onclick='pauseSong()'>
                        <span class='trackNumber' v-if="track">{{ track.track_position }}</span>
                    </div>                      
                        <div class='trackInfo'>
                            <span class='trackName' v-if="track">{{ track.title }}</span>
                        </div>  
                    <div class='trackOptions'>
                        <img class='optionsButton' src='/assets/images/icons/more.png'>
                    </div>

                    <div class='trackDuration'>
                        <span class='duration' v-if="track">{{ track.duration }}</span>
                    </div>
                </li>
                <div class="rLabel" v-if="album">
                    &copy; {{ album.albumDetails.recordlabel }}
                </div>
            </ul>

and i am getting this error on chrome console [Vue warn]: Error in render: "TypeError: Cannot read property 'track' of null"

What am i doing wrong?

alan9608's avatar

It seems that you have introduced a variable 'track' that does not show up in the code you are showing. The error says the same thing when it says it "Cannot read property 'track' of null". Where does 'track' get introduced to the code?

alan9608's avatar

It is the first use of 'track' that is confusing. v-if="track" refers to nothing at this time so it is null.

Please or to participate in this conversation.