put方法不在前端工作,但在邮递员中起作用

发布于 2025-02-09 00:48:52 字数 2471 浏览 2 评论 0原文

请帮我。我正在做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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-02-16 00:48:52

您在哪里设置todoput请求用新值代替当前值。如果仅设置Description属性,但请将todo out放置,它将用一个空字符串覆盖它,清除您中的所有数据。要么在制作put请求之前设置todo的旧值,要么改用patch代替。一个补丁仅影响某些选定的值而不是所有内容。无论如何,仍然有可能使用put进行此操作。

尝试创建一个使用效率,以将其设置为待命。

类似的事情:

    const todoVar = description.find((desc)=> desc.id === todo.id);
    
    useEffect(()=>{
      if(todoVar){
        setDescription(description.todo)
      }
    },[todoVar, setDescription])

基本上,您要在制作put> put请求之前将todo值从数据库中设置为原始值。这样,它就不会空/空。

Where do you set the todo? A put request replaces the current values with new ones. If you only set the description property but leave the todo out, it will overwrite it with an empty string, clearing any data you had in there. Either set the old value of the todo before making your put request or use a patch instead. A patch only affect certain selected values instead of everything. Anyway, it's still very possible to do this with a put instead.

Try creating a useEffect where you set your todo to state.

Something like this:

    const todoVar = description.find((desc)=> desc.id === todo.id);
    
    useEffect(()=>{
      if(todoVar){
        setDescription(description.todo)
      }
    },[todoVar, setDescription])

Basically, you want to set the todo value from your database to it's original value before making your PUT request. This way it won't be empty/null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文