javascript - 为什么我在修补数据时返回 null
问题描述
我正在修补我的数据,但是当我这样做时,我的数据最终的值为null
. 我什console.log
至发送到我的数据库的值并且我得到了正确的值,但由于某种原因在我的数据库中它显示为空。
, "going":null, "date":"2020-11-10T17:34:11.135Z", "__v":0},
ivs","going":2, "date":"2020-11-11T04:19:24.433Z", "__v":0}]
正如你所看到的,“going”应该有一个整数作为它的值,而不是我修补的那个显示null
。
对于如何解决这个问题,有任何的建议吗?
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>
);
}
}
解决方案
推荐阅读
- c++ - 模板争吵
- android - React Native 仅在 Android 上不响应触摸
- excel - 从另一个 Excel 宏运行 Excel VBA 宏-错误:找不到 PERSONAL.XLSB
- r - 导入 Excel 工作表和工作簿
- python - 如何在Python字典中按键计数
- javascript - 如何遍历 JSON 文件
- reactjs - dotenv-webpack 环境变量不适用于 Github 操作
- android - 在 android 上打开具有 ACTION_VIEW 意图的文件时显示不同的文件名
- python - 在python中将str转换为YYYYmmdd格式
- css - 为什么 Mp4 视频正在下载而不是在我的 WordPress 上播放