axios.js?bb36:705 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'message')
时间: 2023-12-18 21:29:43 浏览: 38
这个错误通常是由于在Promise中没有正确处理错误而导致的。在axios中,如果请求被拒绝或出现错误,它会返回一个rejected状态的Promise。因此,您需要在Promise中添加一个catch块来处理错误。您可以在axios请求拦截器中添加以下代码来处理错误:
```javascript
return config
service.interceptors.response.use(
response => {
return response
},
error => {
console.log(error.message)
return Promise.reject(error)
}
)
```
这将在请求被拒绝或出现错误时打印错误消息,并返回一个rejected状态的Promise。请注意,您需要将其添加到响应拦截器中,而不是请求拦截器中。
相关问题
axios.min.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'indexOf')
在你提供的引用中,主要讨论了axios的使用和加载方式以及在请求拦截器中的配置。从错误提示来看,可能是因为在axios.min.js文件中的某处尝试读取了一个未定义的属性'indexOf',导致了TypeError错误。
根据引用中的提示,正确的axios加载方式是通过将axios赋值给Vue.prototype.axios来实现。而引用中提到,如果是非Vue官方库或者不是为Vue编写的插件,则不支持使用Vue.use()方式加载,而应该使用new Vue()方式。
此外,引用中提到在axios请求拦截器中添加代码return config可以对请求进行拦截和配置。
综上所述,如果你遇到了"Cannot read properties of undefined (reading 'indexOf')"的错误,可能是由于axios的加载方式或请求拦截器配置问题导致的。你可以检查代码中是否使用了正确的加载方式,并确保在请求拦截器中正确配置了拦截和返回config的代码。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* *2* [Vue报错: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘protocol‘)](https://blog.csdn.net/weixin_55971433/article/details/125529573)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
- *3* [Vue报错:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘cancelToken...](https://blog.csdn.net/weixin_42343966/article/details/128454897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
[ .reference_list ]
Axios.js:51 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'endsWith')
根据你提供的引用内容,出现错误"Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'endsWith')"的原因是在Axios.js文件的第51行尝试读取一个未定义的属性'endsWith'。这个错误通常发生在使用旧版本的浏览器或者不支持ES6的环境中。
解决这个问题的方法是更新你的浏览器版本或者使用一个支持ES6的环境。另外,你也可以尝试使用polyfill来解决这个问题。Polyfill是一个用于填充浏览器不支持的功能的代码片段。
以下是一个使用polyfill解决这个问题的例子:
```javascript
// 引入polyfill
import 'core-js/stable';
import 'regenerator-runtime/runtime';
// 在你的代码中使用endsWith方法
const str = 'Hello, world!';
console.log(str.endsWith('world')); // 输出:true
```
使用polyfill可以确保你的代码在不同的浏览器和环境中都能正常运行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)