簡體   English   中英

添加錯誤捕獲后,Axios-POST引發錯誤:ReferenceError:未定義axiosRequest

[英]Axios-POST throws error after adding error catch: ReferenceError: axiosRequest is not defined

我在axios請求的catch函數中添加了一個錯誤日志記錄。 自從我這樣做以來,我一直收到這個奇怪的錯誤

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;
          })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM