put方法不在前端工作,但在邮递员中起作用
请帮我。我正在做Pern stack应用程序,我不明白为什么我的PUT方法在前端不起作用,而是在Postman中工作。当我添加一个todo时 - 例如,在数据库ID中出现“饮用水”时,也会出现尾巴,但是当我在前端编辑该托多(Todo)时,数据库和前端也为无效。 ,,,, 看起来像这样:描述{ID:56,todo:null}
这是Edittodo.js的代码:
export default function EditTodo({ todo }) {
const [description, setDescription] = useState(todo);
console.log("todos", todo);
console.log("description", description);
// Edit function
const editText = async (id) => {
try {
const body = { description };
const response = await fetch(`http://localhost:5000/todos/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
console.log("response", response);
window.location = "/";
} catch (error) {
console.error(error.message);
}
};
return (
<Fragment>
<button
type="button"
className="btn btn-warning"
data-toggle="modal"
data-target="#myModal"
>
Edit
</button>
<div className="modal" id="myModal">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">Edit Todo</h4>
<button
type="button"
className="close"
data-dismiss="modal"
></button>
</div>
<div className="modal-body">
<input
type="text"
className="form-control"
value={description.todo}
onChange={(e) => setDescription(e.target.value)}
/>
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-warning"
data-dismiss="modal"
onClick={() => editText(todo.id)}
>
Edit
</button>
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</Fragment>
);
}
在我编辑TODO时,看起来Edittext函数未执行。 提前致谢。
Please, help me. I am doing PERN stack app, and I can't understand why my PUT method is not working in front end but works in Postman perfectly. When I add a todo - "drink water" for example, in database id appears as should "drink water" and in fron end as well it appears, but when I edit that todo in front end it's null in database and front end as well,
looks like that: description {id: 56, todo: null}
Here is a code of EditTodo.js:
export default function EditTodo({ todo }) {
const [description, setDescription] = useState(todo);
console.log("todos", todo);
console.log("description", description);
// Edit function
const editText = async (id) => {
try {
const body = { description };
const response = await fetch(`http://localhost:5000/todos/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
console.log("response", response);
window.location = "/";
} catch (error) {
console.error(error.message);
}
};
return (
<Fragment>
<button
type="button"
className="btn btn-warning"
data-toggle="modal"
data-target="#myModal"
>
Edit
</button>
<div className="modal" id="myModal">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">Edit Todo</h4>
<button
type="button"
className="close"
data-dismiss="modal"
></button>
</div>
<div className="modal-body">
<input
type="text"
className="form-control"
value={description.todo}
onChange={(e) => setDescription(e.target.value)}
/>
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-warning"
data-dismiss="modal"
onClick={() => editText(todo.id)}
>
Edit
</button>
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</Fragment>
);
}
Looks like EditText function is not executed when I edit the todo.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在哪里设置
todo
?put
请求用新值代替当前值。如果仅设置Description
属性,但请将todo
out放置,它将用一个空字符串覆盖它,清除您中的所有数据。要么在制作put
请求之前设置todo
的旧值,要么改用patch
代替。一个补丁仅影响某些选定的值而不是所有内容。无论如何,仍然有可能使用put
进行此操作。尝试创建一个使用效率,以将其设置为待命。
类似的事情:
基本上,您要在制作
put> put
请求之前将todo
值从数据库中设置为原始值。这样,它就不会空/空。Where do you set the
todo
? Aput
request replaces the current values with new ones. If you only set thedescription
property but leave thetodo
out, it will overwrite it with an empty string, clearing any data you had in there. Either set the old value of thetodo
before making yourput
request or use apatch
instead. A patch only affect certain selected values instead of everything. Anyway, it's still very possible to do this with aput
instead.Try creating a useEffect where you set your todo to state.
Something like this:
Basically, you want to set the
todo
value from your database to it's original value before making yourPUT
request. This way it won't be empty/null.