简体   繁体   中英

getting a 400 status when doing a patch request with axios

I'm building a front end with react and one of the components needs to take accept a patch request to increment or decrement the vote.

Can anybody spot a reason why I might be getting a 400 for this code.

handleIncrementVote = () => {
    const { commentid, articleid } = this.state;

    const vote = {
        inc_votes: this.state.votes
    }

    axios.patch(`https://lloyd- 
   news.herokuapp.com/api/articles/${articleid}/comments/${commentid}`, 
   { 
   vote })
    .then(res => {
        console.log(res);
        this.setState((prevState) => ({
            inc_votes: prevState.votes + 1
        }), () => {
            console.log(this.state.votes)
        })
    }).catch(err => console.log(err))

When I send the request with postman it works fine. Any ideas? Does my syntax look ok?

Solved. My bad! I was passing in this.props rather than this.state in my object. Thanks for everyones suggestions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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