Home > Software design >  TypeError: Cannot read properties of undefined (reading 'token')
TypeError: Cannot read properties of undefined (reading 'token')

Time:05-12

when i use localStorage i have this error : Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'token'). But my token is here :

enter image description here

Promise is here :

enter image description here

        async loginSubmit () {
        const response = axios.post('http://localhost:3000/api/auth/login', {
            email: this.email,
            password: this.password
        });
        localStorage.setItem('token', response.data.token);
        console.log(response);
        this.$router.push('actu');

    },

Can you help me ? please

CodePudding user response:

Axios returns a promise, you need to await the request:

const response = await axios.post(...)

CodePudding user response:

You are using axios, it returns a promise. That's why you have this error. You have to use async await to wait for axios to respond

const response = await axios.post('http://localhost:3000/api/auth/login', {
            email: this.email,
            password: this.password
        });
  • Related