简体   繁体   English

React导入的库抛出错误:无法将undefined或null转换为object

[英]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.

相关问题 使用可加载库的 SSR React 应用程序,在主路由的服务器上间歇性地抛出“无法将未定义或 null 转换为对象”错误 - SSR React Application using loadable library, throws “Cannot convert undefined or null to object” error intermittently on the server for Home Route 反应:错误:无法将未定义或 null 转换为 object - React: error: Cannot convert undefined or null to object UglifyJs引发错误:无法将未定义或null转换为对象 - UglifyJs throws error: Cannot convert undefined or null to object 无法将未定义或null转换为React中的对象 - Cannot convert undefined or null to object in react TypeError:无法在反应中将未定义或 null 转换为 object - TypeError: Cannot convert undefined or null to object in react 错误:无法将未定义或 null 转换为 object - Error: Cannot convert undefined or null to object 错误TypeError:无法将未定义或null转换为对象 - error TypeError: Cannot convert undefined or null to object 无法将未定义或空值转换为对象的错误 - Error with Cannot convert undefined or null to object React JS:TypeError:无法将未定义或空值转换为对象 - React JS: TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为 object React JS - Uncaught TypeError: Cannot convert undefined or null to object React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM