javascript - axios get 请求无法将 undefined 转换为对象

标签 javascript axios svelte

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

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

Uncaught TypeError: can't convert undefined to object

mergeConfig mergeConfig.js:92 request Axios.js:39 method Axios.js:129 wrap bind.js:9 login Login.svelte:11 listen index.mjs:412 listen_dev index.mjs:1961 mount bundle.js:3413 mount_component index.mjs:1745 update bundle.js:765 update bundle.js:931 update index.mjs:1075 flush index.mjs:1042 promise callback*schedule_update index.mjs:1000 make_dirty index.mjs:1777 ctx index.mjs:1815 unsubscribeLoc bundle.js:1442 subscribe index.mjs:50 instance$3 Router.svelte:493 init index.mjs:1809 Router bundle.js:1583 create_fragment bundle.js:3583 init index.mjs:1824 App bundle.js:3655 app 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>

这也不行。 我产生了与上述相同的错误。

最佳答案

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

关于javascript - axios get 请求无法将 undefined 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70365170/

相关文章:

svelte - 如何将每个循环中的项目传递到函数中

javascript - 为类名引用的表类添加按按钮类名的onclick监听器

javascript - JS 将背景位置设置为数组中的随机数

javascript - 如何使用 sinon 模块模拟 axios 请求

javascript - 如何使用 Entity Framework 传入 id 并路由到我的 Controller

vue.js - axios已定义但从未使用

javascript - 从对象数组中的属性创建字符串文字联合

svelte - 如何在 svelte 中模板化内联属性?

javascript - Ember.js:对象模型和模型有什么区别?

javascript - 如何让 chrome 停止自动填写各种形式的电子邮件地址和密码?