Basically I'm using Vue 3 and I try to assign an error returned by the server, for example if the email has been taken by another user. It worked fine the first time, I could assign the message returned by the server to my state.responseErrorMessage
property, but when I try to resend with a new email without refreshing the page, the ERROR_MESSAGE mutation does not update !
<script> computed: { ...mapState(['responseSuccessMessage','responseErrorMessage']), }, methods: { ... if( this.errors.firstname.errMsg === null && this.errors.lastname.errMsg === null && this.errors.email.errMsg === null && this.errors.password.errMsg === null && this.errors.passwordConfirm.errMsg === null && this.errors.terms.errMsg === null) { this.$store.dispatch('creatAccount', { firstName: this.firstname, lastName: this.lastname, email: this.email, password: this.password }) setTimeout(() => { // first time submitting the form it display the error message but the second time it doesn't ! console.log(this.responseErrorMessage) }, 2000) } } </script>
export default createStore({ state: { responseSuccessMessage: null, responseErrorMessage: null }, mutations: { SUCCESS_MESSAGE(state, message) { state.responseSuccessMessage = message }, ERROR_MESSAGE(state, message) { state.responseErrorMessage = message setInterval(() => { state.responseErrorMessage = null }, 3000) } }, actions: { async creatAccount({ context, commit, }, user) { try { let result = await axios.post('http://localhost:3001/api/auth/signup', { firstName: user.firstName, lastName: user.lastName, email: user.email, password: user.password }) if (result.status === 201) { commit('SUCCESS_MESSAGE', result.data.message) // state.responseSuccessMessage = result.data.message } } catch (err) { if (err.response.status === 409) { context, commit, // Put this line to avoid eslint errors! commit('ERROR_MESSAGE', err.response.data.message) } else { console.log(err) } } } }, modules: {} })
Please do not use asynchronous methods (setInterval, setTimeout, promise, ajax...) in your mutation function, you can change your code, code
You may want to reset responseErrorMessage to null, but the wrong way, you can write like this:
mutation:
Global Observation:
You can try