简体   繁体   English

axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误”

[英]axios: Unhandled Rejection (TypeError): Cannot read property 'error' of undefined

I am trying to use axios to make a POST request.我正在尝试使用 axios 发出 POST 请求。 Following is my axios function in a file called userFunctions.js.以下是我在名为 userFunctions.js 的文件中的 axios function。

export const savePost = (postInfo) => {
    return axios
        .post(
            "admin/savepost",
            { first_name: "hello", last_name: "world" },
            {
                headers: {
                    Authorization: `Bearer ${localStorage.usertoken}`,
                },
            }
        )
        .then((response) => {
            console.log(response); //response is able to get printed here
            console.log("axios:: saved post");
        });
};

I'm calling this in another file called card.js shown below:我在另一个名为 card.js 的文件中调用它,如下所示:

handleChange(e) {
        e.preventDefault();
        const { name, type, checked } = e.target;
        if (type === "checkbox") {
            this.setState({
                [name]: checked,
            });
            savePost("post 1").then((res) => {
                //response is undefined here!
                console.log(res === undefined); //true

                if (!res.error) {
                    console.log("client: post saved");
                }
            });
        }
    }

and I'm getting the following error:我收到以下错误:

Unhandled Rejection (TypeError): Cannot read property 'error' of undefined未处理的拒绝(TypeError):无法读取未定义的属性“错误”

I don't understand is why the response is able to get printed in the userFunctions.js but is undefined when I call the savePost variable in card.js我不明白为什么响应能够在 userFunctions.js 中打印但在我调用 card.js 中的 savePost 变量时未定义

It happens because you handle response in the userFunctions.js and do not return it back(ie last then in your promise chain returns undefined).发生这种情况是因为您在 userFunctions.js 中处理响应并且不将其返回(即then在您的 promise 链中返回未定义)。 Make following change:进行以下更改:

    .then((response) => {
        console.log(response); //response is able to get printed here
        console.log("axios:: saved post");
        return response
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined React - 未处理的拒绝(TypeError):无法读取未定义的属性“错误” - React - Unhandled Rejection (TypeError): Cannot read property 'error' of undefined React API错误(未处理的拒绝(TypeError):无法读取未定义的属性“ 0”) - React API Error (Unhandled Rejection (TypeError): Cannot read property '0' of undefined) 未处理的拒绝(TypeError):尝试在 ReactJS 上使用 Axios 加载 API 数据时,无法读取未定义的属性“0” - Unhandled Rejection (TypeError): Cannot read property '0' of undefined happening while trying to load API data with Axios on ReactJS 未处理的拒绝(typeError):无法在 google chrome mobile 上使用 reactjs axios 读取未定义的属性 - Unhandled Rejection (typeError): Cannot read property of undefined using reactjs axios on google chrome mobile 反应 - Redux | 未处理的拒绝(类型错误):无法读取未定义的属性“数据” - React - Redux | Unhandled Rejection (TypeError): Cannot read property 'data' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性“ id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Reactjs + Redux 未处理的拒绝(TypeError):无法读取未定义的属性“数据” - Reactjs + Redux Unhandled Rejection (TypeError): Cannot read property 'data' of undefined 未处理的拒绝(类型错误):无法读取未定义的属性“推送” - Unhandled Rejection (TypeError): Cannot read property 'push' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“数据” - React- Unhandled Rejection (TypeError): Cannot read property 'data' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM