我正在修补数据,但是当我这样做时,数据最终以null值结束。我什至console.log x(发送到数据库的值)也得到了正确的值,但是由于某种原因,在数据库中它显示为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>
);
}
}