修补数据时为什么返回null

时间:2020-11-12 01:27:46

标签: javascript reactjs mongodb express axios

我正在修补数据,但是当我这样做时,数据最终以null值结束。我什至console.log x(发送到数据库的值)也得到了正确的值,但是由于某种原因,在数据库中它显示为null。 As you can see, "going" should have an integer as its value, but instead the one that I patched shows null 如您所见,“ going”的值应为整数,但我修补的那个值显示为空

关于如何解决此问题的任何建议?

import React from 'react';
import './App.css';
import axios from "axios";



export default class Scroll extends React.Component {

    state = {
        posts: [],
        id: 0
    }


    componentDidMount() {
        axios.get('http://localhost:5000/posts/save')
            .then(res => {
                const posts = res.data;
                this.setState({ posts });
            })
    }


    handleClick = (id) => {
        var i;
        for (i = 0; i < this.state.posts.length; i++) {
            if (this.state.posts[i]._id === id) {
                let x = this.state.posts[i].going + 1;
                axios({
                    url: `http://localhost:5000/posts/save/${id}`,
                    method: 'PATCH',
                    data: x,
                })
                    .then(() => {
                        console.log(x);
                    })
                    .catch(() => {
                        console.log('Internal server error');
                    });
            }
        }


    }

    render() {

        return (
            <div className="flex-container">
                {
                    this.state.posts.reverse().map((posts) =>
                        <div className="post">
                            <h3 id="post-text">{posts.title}</h3>
                            <p id="post-text">{posts.body}</p>
                            <p>{posts.going}</p>
                            <button onClick={() => this.handleClick(posts._id)}>Going</button>
                        </div>

                    )}
            </div>
        );
    }
}

0 个答案:

没有答案