【问题标题】:Axios promise will never resolveAxios 承诺永远不会解决
【发布时间】:2019-11-29 14:20:56
【问题描述】:

在我的一生中,我永远无法让我的 Axios.post 承诺解决。

我知道我的前端和后端是完美连接的。

尝试/捕获块以返回已解决的承诺也没有奏效。

无论我做什么,我都无法进入我的 promise.then() 函数。我在后端文件中做错了什么?

无法兑现承诺的代码

async handleDateSubmit() {
    let resolvedPromise = await Axios.post(
        "http://localhost:3001/get_number_of_dates_from_email",
        {
            email: this.state.user_email_m
        }
    );

    resolvedPromise
        .then(response => {
            //I can never get to here.
            console.log("Made it inside");
        })
        .catch(err => console.log(err));
}

//---attempt two----//
async getResolvedPromise() {
    try {
        return await Axios.post(
            "http://localhost:3001/get_number_of_dates_from_email",
            {
                email: this.state.user_email_m
            }
        );
    } catch (error) {
        console.log(error);
    }
}

async handleDateSubmit() {
    let resolvedPromise = this.getResolvedPromise();

    //work with resolvedPromsie
}

当前代码

//------------send_info.js front end file----------//
handleDateSubmit() {
    Axios.post('http://localhost:3001/get_number_of_dates_from_email', {
        email: this.state.user_email_m
    })
    .then((response) => {
        //I can never get to here.
        console.log("Made it inside");
    })
    .catch(err => console.log(err));
}

//---------------server.js backend file---------------//
router.route('/get_number_of_dates_from_email').post(function (req, res) {
    //"user_email" is correct in my schema model and "req.body.email" is always what it should be
    User.findOne({ user_email: req.body.email }, (err, foundUser) => {
        console.log("Inside of findOne()");

        if (err) {
            return res.send(err);
        }
        else {
            let numDates = foundUser.dates_list.length;

            //I always get here and numDates is always correct
            console.log("Number of dates: ", numDates);
            return res.json({ "numDates": numDates }); //Should I be using res.send()? 
        }
    });
});

【问题讨论】:

    标签: reactjs mongodb mongoose promise axios


    【解决方案1】:

    您的代码中似乎有时会混淆承诺和已解决的承诺

    // Attempt one
    async handleDateSubmit() {
        try {
            let resolvedPromise = await Axios.post(
                "http://localhost:3001/get_number_of_dates_from_email",
                {
                    email: this.state.user_email_m
                }
            );
            // Here resolvedPromise as stated by its name is not a promise anymore, thus you can't use .then()
            // You can directly work with resolvedPromise as it contains the response.
    
        }   catch (e) {
            console.error(e)
        }
     }
    
    // Attempt two
    async getResolvedPromise() {
        try {
            // Here you're returning the resolved promise, but the async await syntax turn your function into an AsyncFunction object
            // This type of function will wrap the return value in a promise if it's not one
            return await Axios.post(
                "http://localhost:3001/get_number_of_dates_from_email",
                {
                    email: this.state.user_email_m
                }
            );
        } catch (error) {
            console.log(error);
        }
    }
    
    async handleDateSubmit() {
        // Thus you need to await the result of your function 
        let resolvedPromise = await this.getResolvedPromise();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多