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

时间:2021-05-05 23:52:40

标签: javascript axios mern

按钮点击监听事件: 更新状态,向 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));
        });

0 个答案:

没有答案