javascript - Axios-POST 添加错误捕获后抛出错误 : ReferenceError: axiosRequest is not defined

标签 javascript vue.js error-handling axios

我在 axios 请求的 catch 函数中添加了一个错误记录。自从我这样做以来,我得到了这个 weird error :

03:21:09.131 Setting 1,2,3,4 on SER1 Visualization.vue:174
03:21:09.132 Object { pin: (4) […], port: "SER1" } Visualization.vue:179
03:21:09.413
Error pin is not defined Visualization.vue:211
    axiosRequest Visualization.vue:211
03:21:09.415 Test Visualization.vue:213
03:21:09.415
undefined Visualization.vue:214
    axiosRequest Visualization.vue:214

注意:axios-post请求不成功。这不是服务器的原因,因为当直接从 sendByte() 调用 axios.post 时它起作用了。

编辑:重建代码后错误消失了。该错误以某种方式从上一次再次删除的提交中持续存在。由于我想了解错误,所以我提供了下面的代码

这是执行 axios 请求的函数:

axiosRequest(payload, path, request) {
      if (request === "POST") {
        axios
          .post(path, payload)
          .then(response => {
            console.debug(
              `${path}? \n Status-Code ${response.status} - ${response.data.status} [${pin}]@${port}`
            );
            this.response = response.data;
          })
          .catch(error => {
            // Error 😨
            if (error.response) {
              /*
               * The request was made and the server responded with a
               * status code that falls out of the range of 2xx
               */
              console.error(error.response.data);
              console.error(error.response.status);
              console.error(error.response.headers);
            } else if (error.request) {
              /*
               * The request was made but no response was received, `error.request`
               * is an instance of XMLHttpRequest in the browser and an instance
               * of http.ClientRequest in Node.js
               */
              console.error(error.request);
            } else {
              // Something happened in setting up the request and triggered an Error
              console.error("Error", error.message); // -> 211
            }
            console.debug("Test"); // -> 213
            console.error(error.config); // -> 214
          });
      }

这里是我调用它的方法:

sendByte(pin, port) {
      console.debug(`Setting ${pin} on ${port}`);
      var payload = {
        pin,
        port
      };
      console.debug(payload);
      this.axiosRequest(payload, this.paths.shift, "POST");
    },

有人能说出这到底是什么意思吗?

最佳答案

第一个问题是由于运行的代码不是我在编辑器中看到的最新代码造成的。我不应该过分相信热插拔。

在文件历史记录中回到 axiosRequest 函数仍然存在的位置后,我重建并在我的代码中发现了错误。

这很简单:

.then(response => {
            console.debug( // here is the Reference Error -> Fix: [${payload.pin}]@${payload.port}
              `${path}? \n Status-Code ${response.status} - ${response.data.status} [${pin}]@${port}`
            );
            this.response = response.data;
          })

关于javascript - Axios-POST 添加错误捕获后抛出错误 : ReferenceError: axiosRequest is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898427/

相关文章:

javascript - "PrimeFaces is not defined"错误

Vue.js 输入建议在输入聚焦和@click 时可见

javascript - Nuxtjs ERROR 无法在将 header 发送到客户端后设置 header

c++ - 如何使用 FormatMessage 函数填充系统错误消息的 va_list?

javascript - 使用 flow.js + ng-flow 将文件上传到 WebAPI 2

javascript - 如何在 javascript 验证失败时不运行 php 脚本?

javascript - 寻找一种在 javascript 中对对象数组进行排序的方法

javascript - 我怎样才能用这个 axios 代码实现拦截器

php - 无法显示PHP错误

error-handling - Common Lisp异常后如何继续正常执行?