[Vuejs]-Handling errors from Express API with Vue

8๐Ÿ‘

โœ…

You must use response property or error object:

handleSubmit () {
  this.$http.post('http://localhost:3000/users/create', this.user)
      .then(response => {
          // if on server side you use only status 200, here it's always true
          if (response.status=== 200) {
            router.push('/users');
          }
      })
      .catch(error => {
          // error.response can be null
          if (error.response && error.response.status === 400) {
              console.log(error.response.data.error);
          }
      })
}

See documentation of axios error handling

๐Ÿ‘คAlex

0๐Ÿ‘

Try send instead of json? Also perhaps accessing error.

BACKEND
res.status(400).send({ error: 'Email already exists!' })

FRONTEND
(err) => { console.log(err["error"]) }

Leave a comment