0

I am having an error when I load the page the first time.

I have the following code in that component.

<router-link
    :to="{
        name: 'play',
        params: { token: this.$route.params.token,
                  id: user.id }
        }"
    target="_blank"
    >
        <button class="btn btn-warning fz-btn-play">
             <i class="fas fa-play"></i>
        </button>
</router-link>

when page loads first-time user.id is giving error because when it loads user variable is null. error is TypeError: Cannot read property 'id' of null

then I run created() method:

created() {
   axios.get("/api/getUser").then((response) => {
       this.user = response.data;
   });
},

when page is fully loaded it is working fine but the error is showing in console how to remove this error.

1 Answer 1

1

Apply v-if on your router-link

<router-link
    v-if="user" //use user as v-if
    :to="{
        name: 'play',
        params: { token: this.$route.params.token,
                  id: user.id }
        }"
    target="_blank"
    >
        <button class="btn btn-warning fz-btn-play">
             <i class="fas fa-play"></i>
        </button>
</router-link>

This will prevent the error you are facing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.