微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MERN 堆栈,axios 将当前状态发布到 DB 错误 400 错误请求

如何解决MERN 堆栈,axios 将当前状态发布到 DB 错误 400 错误请求

按钮点击监听事件: 更新状态,向 mongoDB 发送 axios post 请求 错误堆栈: xhr.js:177 POST http://localhost:3000/auction-items/609204cd33b39d50c8181368/edit 400(错误请求) dispatchXhrRequest @ xhr.js:177 xhrAdapter @ xhr.js:13

Timer.js:189 Error: Request Failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)

组件


    // /////////////////////////////////////////
    // Form Events,Listeners,and err handlers
    // ////////////////////////////////////////
    onFormSubmit = (event) => {
        event.preventDefault();
        if (this.state.auctionEnded === false) {
            return;
        };
    };

    onButtonClick = async (event) => {
        if (this.state.auctionEnded === false) {
            // Current Bidder and Price set to state to be stored in DB
            this.currentPrice = this.state.auctionPrice + .01
            this.formattednum = parseFloat(this.currentPrice)
            this.setState({ highBidder: 'jonny',auctionPrice: this.formattednum })

            await axios.post(`http://localhost:3000/auction-items/${this.state.auctionId}/edit`,{
                highBidder: this.state.highBidder,auctionPrice: this.state.formattednum
            },{
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            })
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    console.log(error);
                });
            
            // Clock Reset
            
            this.reset()
        }
    }

Server.js


    router.post('/:id/edit',function(req,res){
        AuctionItems.findById(req.params.id)
        .then(auctionItem => {
            auctionItem.highBidder = req.body.highBidder
            auctionItem.auctionPrice = parseFloat(req.body.auctionPrice)

            auctionItem.save()
                .then(() => {
                    console.log('Done Auction')
                    res.json('Auction updated!')
                })
                .catch(err => res.status(400).json('Error: ' + err));
            })
            .catch(err => res.status(400).json('Error: ' + err));
        });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。