[英]React imported library throws error: cannot convert undefined or null to object
I'm working with a react application, and I'm calling an action that calls a function to delete a key off the axios
object. 我正在使用react应用程序,我正在调用一个函数来调用一个函数来删除axios
对象中的一个键。 In the Dev Tools Source window, I see that I'm importing axios
but in the development tools, it's showing it as undefined
, and I can't figure out why. 在Dev Tools Source窗口中,我看到我正在导入axios
但在开发工具中,它显示为undefined
,我无法弄清楚原因。
Here is the file that is the root of the issue: 这是作为问题根源的文件:
import axios from 'axios';
const setAuthToken = token => {
if(token) {
axios.defaults.headers.common['Authorization'] = token;
} else {
delete axios.defaults.common['Authorization'];
}
};
export default setAuthToken;
The Error that occurs is displayed on my react app like this: 发生的错误显示在我的反应应用程序上,如下所示:
TypeError: Cannot convert undefined or null to object setAuthToken http://localhost:3000/static/js/main.chunk.js:2525:66 TypeError:无法将undefined或null转换为对象setAuthToken http:// localhost:3000 / static / js / main.chunk.js:2525:66
The line in question is the line inside the else
block. 有问题的行是else
块内的行。
Everything required is imported, but I'm not sure why this error is being thrown. 所需的一切都是导入的,但我不确定为什么会抛出这个错误。
Change 更改
delete axios.defaults.common['Authorization'];
to 至
delete axios.defaults.headers.common['Authorization'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.