search

Home  >  Q&A  >  body text

How to update vuex status after every form submission?

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粉592085423P粉592085423266 days ago434

reply all(1)I'll reply

  • P粉092778585

    P粉0927785852024-03-31 11:12:33

    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

    reply
    0
  • Cancelreply