axios TypeError:无法读取 express 中未定义的属性“协议”

你好

首先,我现在在 html 中使用 cdn script 标签来使用 axios。

在中api.js,我创建了这样的 axios 实例:

const api = axios.create({
  baseURL: "/api",
});

export default {
  test: () => api.get("/test/results"),
};

我在另一个 js 文件 ( test.js) 中使用它,如下所示:

import API from "/api.js";

const getSearchData = async () => {
  try {
    const res = await axios(API.test());
    console.log(res);
  } catch (error) {
    console.error(error);
  }
};

然后我遇到了以下错误并且也console.log(res);没有工作。

TypeError: Cannot read property 'protocol' of undefined
    at isAxiosError.js:10
    at isAxiosError.js:10
    at new Promise (<anonymous>)
    at e.exports (isAxiosError.js:10)
    at e.exports (isAxiosError.js:10)
    at async getSearchData (test.js:17)

但是在网络面板(chrome浏览器)中,它运行得很好。它的状态码是 200。

Request URL: http://localhost:3000/api/test/results
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:3000
Referrer Policy: strict-origin-when-cross-origin

我无法理解。我该如何解决?我不知道怎么了...

达尼拉

你不需要在axios这里第二次打电话await axios(API.test());api已经是axiosaxios.create之前使用call制作实例

所以只需将代码更改为:

import API from "/api.js";

const getSearchData = async () => {
  try {
    const res = await API.test();
    console.log(res);
  } catch (error) {
    console.error(error);
  }
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法读取未定义的属性“协议”:axios react

Axios:未捕获(已承诺)TypeError:无法读取未定义的属性“协议”

Axios在React中未定义,错误:Uncaught TypeError:无法读取未定义的属性“ post”

TypeError:无法读取未定义的React Express的属性'prototype'

nodeJS/Express TypeError:无法读取未定义的属性“方法”

TypeError:无法读取未定义Express的属性“使用”

ReactJS 使用 Ionic Uncaught TypeError:无法从 axios 请求中读取未定义的属性“map”

TypeError:无法读取未定义的属性(读取'then') - axios post nodejs jest

TypeError:无法使用useEffect和Axios读取未定义的属性“ get”

React + Axios:未捕获(承诺)TypeError:无法读取未定义的属性“map”

TypeError:无法使用vue.js和axios读取未定义的属性“ post”

TypeError:无法读取未定义的属性'map'(axios => getData => setState => .map => return elmItem =>错误)

“ TypeError:无法读取未定义的属性'get'”,Axios,Vue.JS

Axios 发布错误 TypeError:无法读取未定义的属性“创建”

Vue.js通过Axios消耗API给出错误:未捕获(已承诺)TypeError:无法读取未定义的属性“协议”

无法读取 axios 包装器未定义的属性“then”

Axios ReactJS-无法读取未定义的属性“ setState”

无法读取未定义的属性“then”-Axios 进入 React

TypeError:无法读取未定义的Node.js Express MVC的属性'Render'

express-openapi-validator:错误:TypeError:无法读取未定义的属性“模式”

Express.js + Mongo:TypeError:无法读取未定义的属性

TypeError:无法读取未定义的属性“协议”

NextJs 中的 Axios api 请求返回无法读取未定义的“地图”的属性

Express.js无法读取未定义的属性“ req”

express multer无法读取未定义的属性“ profileimage”

无法读取未定义的属性“ destroy”-Express JS Session

Express Sequelize错误-无法读取未定义的属性'findAll'

Express:无法读取未定义的属性“forEach”

Express JS:无法读取未定义的属性“ lazyrouter”