【问题标题】:Axios get request can't convert undefined to objectaxios获取请求无法将未定义转换为对象
【发布时间】:2022-01-18 17:26:15
【问题描述】:

我正在尝试在我的 js/svelte 应用程序中发出获取请求。 REST-API 在浏览器中或在使用 postman 进行测试时工作得非常好。

当我点击网站上的 Go 按钮时,检查工具的控制台中会出现以下错误。

Uncaught TypeError: can't convert undefined to object

mergeConfig mergeConfig.js:92 请求 Axios.js:39 方法 Axios.js:129 包装 bind.js:9 登录 Login.svelte:11 听 index.mjs:412 listen_dev index.mjs:1961 挂载 bundle.js:3413 mount_component index.mjs:1745 更新 bundle.js:765 更新 bundle.js:931 更新 index.mjs:1075 刷新 index.mjs:1042 承诺回调* schedule_update index.mjs:1000 make_dirty index.mjs:1777 ctx 索引.mjs:1815 unsubscribeLoc bundle.js:1442 订阅 index.mjs:50 实例$3 Router.svelte:493 初始化索引.mjs:1809 路由器 bundle.js:1583 create_fragment bundle.js:3583 初始化索引.mjs:1824 应用程序包.js:3655 应用程序 main.js:3 bundle.js:3675

这是代码。

    <script>
    import {replace} from 'svelte-spa-router'
    import {LoginDto} from "../scripts/data_transfer_objects/LoginDto";
    import axios from "axios";

    let loginTemplate = new LoginDto();

    function login(){
        console.log(loginTemplate.password);
        axios.get("http://localhost:5000/login", {
            auth: {
                username: "test",
                password: "1234"
            }
        });
        replace("#/activities");
    }
</script>

<div>
    <input type="password" placeholder="Password" bind:value={loginTemplate.password}>
    <button on:click={login}>Go</button>
</div>

有人知道问题出在哪里吗?

提前致谢!

编辑

我尝试了一个更简单的例子。

<script>
    import axios from "axios";

    axios.get("localhost:5000/activities");
</script>

这也不起作用。 我产生与上述相同的错误。

【问题讨论】:

  • 这也不起作用。我很确定通过 auth: {} 和获取请求是可以的。它在使用邮递员时有效。
  • 什么版本的axios?
  • "axios": "^0.24.0",

标签: javascript axios svelte


【解决方案1】:

问题可能来自对@rollup/plugin-commonjs 的过时依赖。从17.0.0 升级到21.0.1 为我解决了这个问题。

【讨论】:

    【解决方案2】:

    尝试记录服务器返回的内容。你可能知道发生了什么。

    axios.get("http://localhost:5000/login", {
      auth: {
               username: "test",
               password: "1234"
            }
    }).then(response => console.log(response));
    

    【讨论】:

    • 请求没有到达服务器,错误发生在手前。但我已经尝试过了,我仍然得到同样的错误。
    猜你喜欢
    • 2021-12-10
    • 2013-08-14
    • 2023-02-21
    • 2021-11-12
    • 2020-01-12
    • 2021-01-23
    • 1970-01-01
    • 2023-03-23
    • 2021-09-01
    相关资源
    最近更新 更多