0

Hey guys I am trying to make a webpage that allows the user to see all the tickets that they can purchase or have purchased. But, the issue is that when the webpage has been loaded the event_id shows undefined within the console. I am not sure as to why this is.

I am using axios to get the event details from a server which is then pushed onto the webpage.

I have placed the code within the mounted function so that everything canbee seen as soon as the page is loaded.

Below is the JavaScript code that I am using.

    data(){
        return{
            allTickets: [],
            getTickets:{
                seeTickets:{
                    event_id: this.event_id
                }
            },
            findTickets: {},
        }
    },

    mounted: function(){
        console.log(this.accessToken)
        axios.get('https://api.ticketpass.co/tickets',{
            headers:{
                "Content-Type" : "application/json",
                "Accept" : "application/json"
            }
        })
        .then(response =>{
            console.log(response);
            console.log(response.data);
            console.log(response.status);
            if(response.status === 200){
                console.log(this.allTickets);
                console.log(response.data.length);
                console.log(response.data[i].event_id);
                for(let i = 0; i < response.data.length; i++){
                    this.allTickets.push({
                        "some_id" : response.data[i].id,
                        "event_id" : response.data[i].event_id,
                        "created" : response.data[i].created,
                        "code" : response.data[i].code,
                        "user_id" : response.data[i].user_id
                    })

                    this.getTickets.seeTickets = {
                        "event_id" : response.data[i].event_id
                    }

                    console.log(!(response.data[i].event_id in this.findTickets));
                    if(!(response.data[i].event_id in this.findTickets)){
                        console.log("Not in Object", response.data[i].event_id, this.findTickets)

                            axios.get('https://api.ticketpass.co/event/' + response.data[i].event_id,{
                                headers:{
                                    "Content-Type" : "application/json",
                                    "Accept" : "application/json",
                                    "Authorization" : this.accessToken
                                }
                            })
                            console.log("Data Added")
                            this.findTickets[response.data.id] = response.data;
                        }

                    else{
                        console.log("IN Object")
                    }
                }
            }
        })

        .catch(error =>{
            console.log(error.response)
        })
    }
}

It would be much appreciated if someone could please help me out.

Many thanks.

Aadil Hafesji
  • 383
  • 2
  • 7
  • 23
  • Please have a look at this. It may be helpful. https://stackoverflow.com/questions/45021907/vue-js-mounted – htoniv Jul 11 '18 at 12:20
  • 1
    `i` is declared on the line after you log `response.data[i]`. So `i` is undefined in this statement. If you want to test the first item, use `response.data[0].event_id` – Thoomas Jul 11 '18 at 12:38
  • @Thoomas Tnx for that. But, the issue is that the all the event_id are being printed as undefined within the console. Meaning that they are not within the findTickets Object. Could you please help. Many thanks. – Aadil Hafesji Jul 11 '18 at 12:43
  • 1
    You're looking for `response.data[i].event_id` to be a key of `this.findTickets`. But you're storing `response.data.id` as a key, not `response.data[i].event_id`. Is it wanted ? Or you're just forgetting to handle the result of your second axios call ? – Thoomas Jul 11 '18 at 13:02
  • Could you please paste all of your `response.data` here? It would be easier for us to help you. – Danon Jul 11 '18 at 13:02
  • @Thoomas Many thanks, that helped – Aadil Hafesji Jul 11 '18 at 13:23

0 Answers0