首页 > 解决方案 > 等待后不运行

问题描述

在第一次等待代码停止运行后,这是我的代码:

handleSubmit = async e => {
    e.preventDefault();

    const { name, number, email, message } = this.state;

    try {
        await axios.post("http://localhost:3001/send", {
            name,
            number,
            email,
            message
        });
    } catch (ex) {
        this.setState({ result: "There was an error, try again." });
    }

    try {
        const response = await fetch("http://localhost:3001/result");
        const data = await response.json();
        this.setState({ result: data });
    } catch (ex) {
       this.setState({ result: "There was an error, try again." });
    }
};

但是第二次尝试中的第二次等待不起作用。有人能帮我吗

标签: reactjsemailasync-await

解决方案


我在我的节点代码中找到了解决方案,我将 res 放在同一个 url (.../send) 并且它有效


推荐阅读