【问题标题】:How can I delete a document with Next.JS/Mongodb using next-connect?如何使用 next-connect 删除带有 Next.JS/Mongodb 的文档?
【发布时间】:2021-08-17 17:37:24
【问题描述】:

我正在使用 NextJS/Mongodb 构建我的第一个 CRUD 应用程序,并且我正在使用 next-connect 作为方法,我对这一切都非常陌生。

我能够成功创建帖子并更新用户个人资料,但我完全无法删除帖子。我试图模仿我创建帖子的方式,但将其切换为.deleteOne 而不是.insertOne。我也可以显示post._id,所以我知道我可以访问它。我只是对如何将它传递给我的删除函数感到困惑。

我知道我应该传入post._id,然后将其发送到位于NextJS 的api/ 文件夹中的handler.delete,然后在handler.delete 中调用我的删除函数。我已经经历了多个使用 next-connect 进行 CRUD 操作的示例,但几乎没有一个示例演示了 delete 操作。或者,也许我只是在寻找错误的地方。我附上了下面的代码,以供参考我目前的位置。

任何帮助将不胜感激。谢谢!

// components/post/posts.jsx

function Post({ post }) {

  const postDelete = (id) => {
    const body = {
      _id: id,
    };
    fetch("/api/posts", {
      method: "DELETE",
      body: JSON.stringify(body),
    });
  };

  return (
    <div>
        {post._id}
        <button onClick={() => postDelete(post._id)}>Delete</button>
    </div>
);
// api/posts/index.js

handler.delete(async (req, res) => {
  console.log("reached handler delete function");
  const deleteResult = await deletePost(req.db, {
    _id: req.post._id,
  });
  return res.json({ deleteResult });
});
// db/posts.js

export async function deletePost(db, { _id }) {
  return db.collection("posts").deleteOne({
    _id,
  });
}

【问题讨论】:

    标签: javascript node.js mongodb next.js


    【解决方案1】:

    回答

    问题是我的变量名。

    // components/post/posts.jsx
    
    const postDelete = async (event) => {
        if (userInfo) {
          const body = {
            postId: post._id,
          };
          const res = await fetch("/api/posts/patch", {
            method: "DELETE",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(body),
          });
          if (res.status === 200) {
            toast.success("Post Deleted!");
          }
        } else {
          toast.error("Please sign-in!");
        }
      };
    
    // api/posts/index.js
    
    handler.delete(async (req, res) => {
      console.log(req.body);
      const del = await deletePost(req.db, {
        postId: req.body.postId,
      });
      return res.status(200).send("Uploaded");
    });
    
    // db/posts.js
    
    export async function deletePost(db, { postId }) {
      return db
        .collection("posts")
        .deleteOne({
          "_id": postId,
        })
        .then(({ value }) => value);
    }
    

    【讨论】:

    • 您好!感谢你的提问。我是 next.js 的新手,也对删除功能感到困惑...我正在查看您的示例,但我不了解 db/posts.js 部分...是数据库模型吗?但在我的模型中只有模式
    【解决方案2】:

    替换

    // api/posts/index.js
    
    handler.delete(async (req, res) => {
      console.log("reached handler delete function");
      const deleteResult = await deletePost(req.db, {
        _id: req.post._id,
      });
      return res.json({ deleteResult });
    });
    

    通过

    // api/posts/index.js
    
    handler.delete(async (req, res) => {
      console.log("reached handler delete function");
      const deleteResult = await deletePost(req.db, req.body._id);
      return res.json({ deleteResult });
    });
    

    【讨论】:

    • 我尝试了您的解决方案,但没有奏效。我在处理函数中添加了一个控制台日志,单击按钮打印控制台日志,但不幸的是没有做任何其他事情。
    • 如果您的项目不是私有的,请发送一个沙箱或一个仓库链接,以便我检查它。
    • 我发现了问题:)。我将在下面添加一个答案以供参考。
    猜你喜欢
    • 2021-06-20
    • 2021-08-19
    • 2021-06-29
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2014-03-05
    相关资源
    最近更新 更多