Home » vue » axios get request depending on user data fails

axios get request depending on user data fails

Posted by: admin November 26, 2021 Leave a comment

Questions:

I’ve set up an auth system with nuxtjs & django.
However, I do not understand nuxtjs completely

      <template>
  <div>
     {{ this.$auth.$storage.state.user.email }}
  </div>
</template>

<script>
import axios from 'axios';
export default {
}
            axios.get('/api/v1/is-success-or-not?email='+this.$auth.$storage.state.user.email)
            .then((response) => {
            if(response.data === "True") {
            console.log("Player is success")
            $nuxt.$router.push('/schedule')
            
            }
           
            })



            axios.get('failureornot?email='+this.$auth.$storage.state.user.email)
            .then((response) => {
            if(response.data > 3) {
            console.log("failure")
            $nuxt.$router.push('/failure')
            }
            })
        </script>

This is my code
I’ve the email that shows up with {{ this.$auth.$storage.state.user.email }}

but when I try to adapt the get request depending on the user, I get this error
Cannot read properties of undefined (reading ‘$auth’)

I do not understand because $auth is obviously defined if I can print the value

Answers:

You are missing the whole mounted function in the script

<template>
  <div>
    {{ this.$auth.$storage.state.user.email }}
  </div>
</template>

<script>
import axios from "axios";

export default {
  mounted() {
    axios
      .get(
        "/api/v1/is-success-or-not?email=" +
          this.$auth.$storage.state.user.email
      )
      .then((response) => {
        if (response.data === "True") {
          console.log("Player is success");
          $nuxt.$router.push("/schedule");
        }
      });

    axios
      .get("failureornot?email=" + this.$auth.$storage.state.user.email)
      .then((response) => {
        if (response.data > 3) {
          console.log("failure");
          $nuxt.$router.push("/failure");
        }
      });
  },
};
</script>