Javascript 为什么我在修补数据时返回null
我正在修补我的数据,但当我这样做时,我的数据将以null值结束。我甚至使用console.log x(我正在发送到数据库的值)来获得正确的值,但由于某种原因,在我的数据库中它显示为null。 正如您所看到的,“Gong”应该有一个整数作为其值,但是我修补的那个显示为null 有没有关于如何解决这个问题的建议Javascript 为什么我在修补数据时返回null,javascript,reactjs,mongodb,express,axios,Javascript,Reactjs,Mongodb,Express,Axios,我正在修补我的数据,但当我这样做时,我的数据将以null值结束。我甚至使用console.log x(我正在发送到数据库的值)来获得正确的值,但由于某种原因,在我的数据库中它显示为null。 正如您所看到的,“Gong”应该有一个整数作为其值,但是我修补的那个显示为null 有没有关于如何解决这个问题的建议 import React from 'react'; import './App.css'; import axios from "axios"; export d
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>
);
}
}
从“React”导入React;
导入“/App.css”;
从“axios”导入axios;
导出默认类。组件{
状态={
员额:[],
身份证号码:0
}
componentDidMount(){
axios.get()http://localhost:5000/posts/save')
。然后(res=>{
const posts=res.data;
this.setState({posts});
})
}
handleClick=(id)=>{
var i;
对于(i=0;i{
控制台日志(x);
})
.catch(()=>{
log(“内部服务器错误”);
});
}
}
}
render(){
返回(
{
this.state.posts.reverse().map((posts)=>
{posts.title}
{posts.body}
{posts.go}
这个.handleClick(posts.\u id)}>正在运行
)}
);
}
}
你的后端是什么样子当我用postman进行修补时,它可以正常工作,所以我不确定后端是否是问题是x
一个数字还是什么类型?您是否将请求作为json发送?通常我做data:JSON.stringify(_data)x是一个数字@Jerrycsending一个对象对我来说会更好,所以data:JSON.stringify({x:x})