【问题标题】:TypeError: Cannot read properties of undefined (reading 'status') in axiosTypeError:无法读取 axios 中未定义的属性(读取 \'status\')
【发布时间】:2023-01-20 00:22:18
【问题描述】:

这是来自 freecodecamp 教程 (https://www.freecodecamp.org/news/how-to-build-react-based-code-editor/) 的代码,该代码用于反应,但我的项目用于下一个 js,当我在反应项目中运行它时,我没有收到此错误,但是当我在下一个 js 项目我得到错误:

 TypeError: Cannot read properties of undefined (reading 'status')

根据错误信息,错误发生的代码。

axios
        .request(options)
        .then(function (response) {
            console.log("res.data", response.data);
            const token = response.data.token;
            checkStatus(token);
        })
        .catch((err) => {
            let error = err.response ? err.response.data : err;
            // get error status
            let status = err.response.status;
            console.log("status", status);
            if (status === 429) {
                console.log("too many requests", status);

                showErrorToast(
                    `Quota of 100 requests exceeded for the Day! Please read the blog on freeCodeCamp to learn how to setup your own RAPID API Judge0!`,
                    10000
                );
            }
            setProcessing(false);
            console.log("catch block...", error);
        });
};

【问题讨论】:

  • 是不是打错了,你先是let error...,然后是err.response.status。应该是error.response.status吗?
  • 不过总的来说,它是说它无法读取未定义变量的status。这意味着在您的代码中,err.response 未定义。在 catch((err)) => { console.log(err) 的顶部尝试控制台日志记录 err,看看是否可以找到 status 属性
  • 如果您删除该 catch 块中除 console.log(err) 之外的所有内容,您是否未定义?我不认为你这样做,否则我很确定错误会说Cannot read properties of undefined (reading 'response')
  • 我想我找到了问题所在,我认为问题在于,在这段代码之前,我试图从 .env 文件中提取一些 api 密钥,但这些密钥并未从那里提取。但是我不确定如何从 .env 文件中获取变量。
  • 很好,也许最好开始一个新问题。祝你好运!

标签: javascript next.js axios rapidapi


【解决方案1】:

看看 Axious documentation。看起来您甚至没有收到回复,这意味着 error.response 未定义。也许请求选项有问题?

【讨论】:

  • 我想我找到了问题所在,我认为问题在于,在这段代码之前,我试图从 .env 文件中提取一些 api 密钥,但这些密钥并未从那里提取。但是我不确定如何从 .env 文件中获取变量。
猜你喜欢
  • 2020-12-18
  • 2022-10-19
  • 2020-07-16
  • 2021-11-26
  • 2021-11-24
  • 2021-12-20
  • 2021-11-27
  • 2022-01-21
  • 2021-12-04
相关资源
最近更新 更多