如何使用@ nuxtjs / axios获取真实的服务器错误?

时间:2020-03-21 02:08:46

标签: axios nuxt.js

Nuxtjs应用程序,带有axios模块和auth模块。 我的注册方法是这样的:

methods: {
    async register() {
      console.log('user:', this.email, ' pass: ', this.password)
      const regInfo = {
        email: this.email,
        password: this.password
      }
      try {
        await this.$axios.$post('/auth/register', regInfo)
        this.$auth.loginWith('local', {
          data: regInfo
        });
        this.$store.dispatch('setSnackbar', {
            text: 'Thanks for signing up!',
            color: 'success'
          });
      } catch (error) {
        console.log('error:', error);   // <<<<<<  error when register fails.
        this.$store.dispatch('setSnackbar', {
          text: 'Unable to register. please try again.',
          color: 'error'
        });
      }
    }

问题是:当注册失败时,服务器会返回一条不错的错误消息,我可以在“网络”标签中看到它,如下所示:

{"statusCode":400,"message":"User already exists"}

但是在我的捕获代码中,我得到了一些丑陋的结果,没有消息。 这是我在控制台中看到的内容:

error: Error: Request failed with status code 400
    at createError (createError.js?2d83:16)
    at settle (settle.js?467f:17)
    at XMLHttpRequest.handleLoad (xhr.js?b50d:61)

为什么?我该如何获取服务器错误?

0 个答案:

没有答案