I've been encountering an issue while trying to sign up or log in as a user from my Nuxt app to an API that is functioning perfectly. The error message
Cannot read property 'then' of undefined
keeps popping up, and I can't figure out the reason behind this error. Another error message that I come across is unknown action type: authenticateUser
.
Previously, I successfully made a post request from the signup page. However, since I am now attempting to store some data, I decided to move the axios request to the store.
Below is the code snippet for reference:
On the signup page, when clicking on the signup button, it triggers the createAccount
method.
data() {
return {
isSignup: true,
form: {
email: null,
fullname: null,
password: null
}
}
},
methods: {
createAccount() {
this.$store.dispatch("authenticateUser", {
isSignup: this.isSignup,
form: this.form
})
.then(() => {
this.$router.push('/loginsuccess')
})
.catch(e => console.log(e))
}
}
In my store/profile.js
file, which is configured as a store module, I have specified the mutations and actions:
export const mutations = {
setToken(state, token) {
state.token = token
},
logIn(state) {
state.loggedin = true
}
}
export const actions = {
async authenticateUser(vuexContext, authData) {
let authUrl = 'https://lookaam.herokuapp.com/signup/'
if (authdata.isLogin) {
authUrl = 'https://lookaam.herokuapp.com/login/'
}
return await this.$axios
.$post(authUrl, authData.form)
.then(result => {
const token = result.data.token
vuexContext.commit('setToken', token)
})
.catch(e => console.log(e))
},
}
In my nuxt.config.js
file, I have the following setup:
modules: [
'@nuxtjs/axios',
],
axios: {
baseURL: process.env.BASE_URL,
Credentials: false
},
Upon clicking the signup button, I immediately see both
unknown action type: authenticateUser
and Cannot read property 'then' of undefined
errors on the console, indicating a failure in communication with the API.
I'm puzzled by this issue and would appreciate any insights into resolving it.