- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试更新帖子。后端的 PUT 请求工作正常,在 Postman 上测试时返回 200 并更新帖子,但是当我尝试在前端更新帖子( react )时,我没有收到任何错误,但更新的帖子没有被更新提交和 更新的字段(标题和正文)为空。 当我 console.log(data)
时更新的值为空在前端,这就是为什么它们没有被发送到后端,但它们在 post
中正确显示的原因.
为什么更新值null
内data
?如何使用新值更新帖子而不是获取空值?data:
post:
更新代码:前端
const EditPost = ({match}) => {
const [values, setValues] = useState({
title: "",
body: "",
error: ""
});
const [post, setPost] = useState({});
const { user, token } = isAuthenticated();
const {
title,
body,
error,
} = values;
const init = (id) => {
read(id).then(data => {
if (data.error) {
setValues({...values, error: data.error})
} else {
setValues({...values,
title: data.title,
body: data.body,
})
setPost({title: values.title, body: values.body})
}
})
}
useEffect(() => {
const id = match.params.id;
init(id);
}, []);
useEffect(() => {
setPost({...values });
}, [values.title, values.body]);
const handleChange = (name) => (event) => {
setValues({ ...values, [name]: event.target.value });
};
const clickSubmit = (event) => {
event.preventDefault();
setValues({ ...values, error: "" });
editPost(match.params.userId, match.params.id, token, post).then((data) => {
if (data.error) {
setValues({ ...values, error: data.error });
} else {
setValues({
...values,
title: "",
body: "",
error: false,
});
console.log(post)
console.log(data)
}
});
};
const newPostForm = () => (
<form onSubmit={clickSubmit}>
<div>
<input
onChange={handleChange("title")} type="text"
name="title"
value={title}
/>
</div>
<div className="form-group">
<textarea
onChange={handleChange("body")}
value={body} name="body"
/>
</div>
<button type="submit">Publish</button>
</form>
);
const showError = () => (
<div
style={{ display: error ? "" : "none" }}>
{error}
</div>
);
return (
<div>
{showError()}
{newPostForm()}
</div>
);
};
export default EditPost;
export const editPost = (userId, id, token, post) => {
return fetch(`${API}/${userId}/${id}/edit`, {
method: 'PUT',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify(post)
})
.then(response => {
return response.json();
})
.catch(err => console.log(err));
};
用户帖子
<Link className="mypost_btn edit_btn" to={`/${_id}/${post._id}/edit`}>
Edit
</Link>
后端代码
exports.edit = (req, res) => {
if (!ObjectID.isValid(req.params.id))
return res.status(400).send(`ID is not valid: ${req.params.id}`)
const {title, body} = req.body
const updatedPost = {title, body }
Post.findByIdAndUpdate(req.params.id, {
$set: updatedPost
}, {new:true}, (error, data) => {
if (error) {
return error
} else {
res.send(data)
console.log(data)
}
})
}
最佳答案
你的问题在这里:
editPost(match.params.userId, match.params.id, token, post)
post
is not defined.
post
未定义,不传递任何数据。因此
title
和
body
等于
null
.您需要做的是,假设根据我在您的代码中看到的内容,定义一个名为
post
的状态变量。 .我认为你打算这样做:
const [post, setPost] = useState({values.title, values.body});
然后确保每当您的
values
更新您的帖子使用
useEffect()
更改,
useEffect(() => {
setPost({...values });
}, [values.title, value.body]);
因此,当您调用
editPost()
时http-put-method,然后post有
value
.它应该工作。
关于node.js - mern - 数据中更新的值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63381633/
我是 Node 和 react 新手,我正在尝试在我的 MERN 应用程序中进行子域路由。我发现我无法在 react 中路由我的子域,所以这让我只剩下 Node 。在node中,有很多方法,比如vho
我正在使用技术堆栈 MERN 创建一个应用程序和 Redux .我想知道如何使用 moongose 检索最后添加到数据库的记录.例如,假设它将是分配给特定用户的订单列表。 我已经在后端完成了一个检索所
我正在构建一个基于 MERN Stack 的费用控制应用程序,我想知道哪种方式是处理数据的最佳方式。 更具体地说,我的费用数据具有以下模型 { "_id" : ObjectId("5ece
我正在尝试根据查询从 Mongo db 获取一些记录。我完成了后端,它与 Postman 配合得很好,但我不知道如何将查询的变量从 React 前端发送到后端。 有一个 Client 模型类,我想检索
我正在尝试更新帖子。后端的 PUT 请求工作正常,在 Postman 上测试时返回 200 并更新帖子,但是当我尝试在前端更新帖子( react )时,我没有收到任何错误,但更新的帖子没有被更新提交和
当我重新加载 Heroku 页面时,出现内部服务器错误,状态代码为 500。 但在我本地的环境中并没有发生这种情况。 除此之外, 当我访问正确重定向并正常显示的路由地址时。 当我重新加载页面时,在 c
我在前端连接了 mongoDB 和 react/redux 的 node/express api。 我如何向用户显示错误(例如数据库连接错误)。 例如,如果后端发生错误,我想在组件中创建带有错误消息的
我正在尝试弄清楚如何有条件地渲染部分 react 。我的条件是,当列表项处于事件状态时,应呈现部分内容。 我正在使用 bootstrap,它有一个可用的事件元素。 我的目标是实现类似于对讲机条款页面的
我的应用程序概览:用户可以提交包含图片和文字的帖子。 我正在使用 MERN 堆栈。 目前,图像正在保存到我的 localhost:3001 中服务器在 /public/uploads/然后将路线保存到
我在 React 中得到了带有表单的简单 MERN 堆栈应用程序,它将字段传递给 redux 状态。 Redux 与后端的 node/express 连接,将数据保存在 MongoDB 中。 表单需要
我需要使用像这样的 mongodb 记录: { "_id": { "$oid": "5c09ae281646e8d8bad07d73" }, "name": "
我正计划将 MERN 堆栈应用程序部署到 heroku(在 youtube 上跟随 Traversy Media),我想知道在客户端隐藏 api key (谷歌地图)的最佳做法是什么? 我知道如何在
我是 Web 开发新手,我有一个托管服务,我想在其上部署我的 ReactJS/Node 项目。之前为了部署它,我只是上传了通过运行 npm run build 创建的构建文件夹。此后,我使用 MERN
我使用 create-react-app 制作了一个非常简单的 MERN 应用程序,没有 mongo 数据库。当我部署到 heroku 时,我看到的只是一个空白页面。我已经尝试了很多没有运气的事情。
我尝试在 MEAN.js 中包含一个中间件 (passport-http-bearer),但它使用与 Express 4 不同的路由语法。 Express API 语法是: app.get('/',
我正在部署 MERN 应用程序,并正在寻找有关构建 API 与 React 路由相对路径的最佳方法的指导。以下是我的两条路线的子集。我的相对路线复制了文件夹结构,我不确定如何解决这个问题。 用户路由
我第一次使用 MERN 堆栈构建应用程序。 为了记录 HTTP 请求,我使用“morgan”。 我设法将数据发送到 mongodb,它似乎工作正常。问题是我的帖子请求没有通过。它显示“待处理”4 分钟
我正在考虑将 MERN 框架用于一个新项目,但到目前为止,我所看到的并没有让我感到鼓舞。我已按照通过 mern-cli 设置项目的说明进行操作,该项目确实已创建并正确运行,但当我执行时... npm
我使用 Node、Express、React 和 MongoDB 创建了一个简单的计费 Web 应用程序 但是我想将它转换为本地机器的桌面应用程序。 我知道可以使用 Electron ,但我不知道该怎
我正在尝试将我的 MongoDB 连接到我的 MERN Web 应用程序,并且想知道有关此类信息的最佳资源。基本上,我有一个 MERN Web 应用程序、 Electron 桌面应用程序和 react
我是一名优秀的程序员,十分优秀!