axios.post.then() is ignoring the then() part for some reason

Viewed 160

I have this function...

function waterCalibrator() {
    axios.post("http://localhost:3001/api/update-water", {
    waterValue: props.moisture
    }).then(function(response){
        console.log("Water calibration worked")
        console.log(response);
    }).catch(function(error) {
        console.log(error);
    })
    props.airValueObject.setWaterFlag(true);
    props.airValueObject.setWaterValue(props.moisture);
}

Can anyone explain why then() is not ever triggered? I have no errors. It simply isn't triggering. Everything works except for this...

    }).then(function(response){
        console.log("Water calibration worked")
        console.log(response);
    }).catch(function(error) {
        console.log(error);
    })

server side looks like this...

app.post("/api/update-water", (req, res) => {
    const waterValue = req.body.waterValue;
    const sqlUpdateWater = "UPDATE user SET waterValue=? WHERE uid='j' ";
    db.query(sqlUpdateWater, [waterValue], (err, result)=>{
        console.log(`water is: ${waterValue}`)
    })
})
1 Answers

You are getting a 204 status code, which means the request went through successfully but there is no content at all, hence you are trying to log nothing.

The HTTP 204 No Content success status response code indicates that the request has succeeded, but... The common use case is to return 204 as a result of a PUT request, updating a resource, without changing the current content of the page displayed to the user. If the resource is created, 201 Created is returned instead. If the page should be changed to the newly updated page, the 200 should be used instead.

You also need, as suggested in previous comments, open the dev console and check the status of the promise itself. This promise seems to be successful.

If this answer helps solving the issue, consider accepting the answer or upvoting it. Thanks.

Related