Axios承诺永远不会解决

时间:2019-07-21 09:33:42

标签: reactjs mongodb mongoose promise axios

对于我一生,我可以从不得到我的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()? 
        }
    });
});

1 个答案:

答案 0 :(得分:0)

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

// 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();
}