How to update vuex status after every form submission?
P粉592085423
P粉592085423 2024-03-30 15:29:17
0
1
448

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 !

My component:

    <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>

want to:

        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: {}
        })

P粉592085423
P粉592085423

reply all(1)
P粉092778585

Please do not use asynchronous methods (setInterval, setTimeout, promise, ajax...) in your mutation function, you can change your code, code

ERROR_MESSAGE(state, message) {
      state.responseErrorMessage = message
      setInterval(() => {
        state.responseErrorMessage = null
      }, 3000)
    }

You may want to reset responseErrorMessage to null, but the wrong way, you can write like this:

mutation:

ERROR_MESSAGE(state, message) {
      state.responseErrorMessage = message
    }

Global Observation:

responseErrorMessage (New,Old){
  if(New){
    setTimeout(()=>{
     this.$store.commit('ERROR_MESSAGE',null)
    },3000)
  }
}

You can try

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template