简体   繁体   English

为什么带有 async/await 的 axios 在这种情况下不起作用?

[英]Why axios with async/await not working in this case?

I wanna change DoorState when click button.我想在单击按钮时更改DoorState

After post API to change DoorState , call get API to check robot state.在发布 API 以更改DoorState后,调用 get API 来检查机器人 state。

Actually DoorState is changed.实际上DoorState改变了。 But not change in console.log(after) code.但不会更改console.log(after)代码。 Why?为什么?

{
    deliveryPost.Delivery.status === 'assigned' &&
        (checkDoorState(deliveryPost.Delivery.postId) === 'closed' ? ( <
            >
            <
            button className = "bg-white text-teal-500 py-2 px-4 ml-2 border border-teal-500 rounded hover:bg-teal-500 hover:text-white"
            onClick = {
                () => clickRobotAction(deliveryPost.Delivery.postId, 'open')
            } >
            Open Door <
            /button> <
            />
        ) : ( <
            >
            <
            button className = "bg-white text-teal-500 py-2 px-4 ml-2 border border-teal-500 rounded hover:bg-teal-500 hover:text-white"
            onClick = {
                () => clickRobotAction(deliveryPost.Delivery.postId, 'close')
            } >
            Close Door <
            /button> <
            />
        ))
}
``
`

`
``
const clickRobotAction = async (postId: string, action: string) => {
    const appliedRobot = robots.find(robot => robot.postId === postId);
    const {
        robotId
    } = appliedRobot;

    try {
        await axios.post(`api/${robotId}/action/${action}`); // DoorState open <=> close
        const after = await axios.get('api/robots');

        console.log(after); // But not change in this log. Why?
    } catch (error) {
        console.error(error);
    }
};

I'm not sure, but I think it is because the axios.post request should be axios.get since you aren't adding a second overload parameter for an object.我不确定,但我认为这是因为 axios.post 请求应该是 axios.get 因为您没有为 ZA8CFDE6331BD59EB2AC96F8911C4B666 添加第二个过载参数Since you are just sending a query string then I think it should just be a get request not certain though I hope this helps.由于您只是发送一个查询字符串,那么我认为它应该只是一个不确定的获取请求,尽管我希望这会有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM