Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Next connect删除具有Next.JS/Mongodb的文档?_Javascript_Node.js_Mongodb_Next.js - Fatal编程技术网

Javascript 如何使用Next connect删除具有Next.JS/Mongodb的文档?

Javascript 如何使用Next connect删除具有Next.JS/Mongodb的文档?,javascript,node.js,mongodb,next.js,Javascript,Node.js,Mongodb,Next.js,我正在用NextJS/Mongodb构建我的第一个CRUD应用程序,我正在使用nextconnect作为方法,我对所有这些都是全新的 我能够成功地创建一个帖子,并更新用户配置文件,但我完全无法删除帖子。我试图模仿我创建帖子的方式,但将其切换到.deleteOne而不是.insertOne。我还可以显示post.\u id以便我知道我可以访问它。我只是对如何将其传递到delete函数感到困惑 我知道我应该传入post.\u id,然后将其发送给处理程序。delete位于NextJS的api/文件夹

我正在用NextJS/Mongodb构建我的第一个CRUD应用程序,我正在使用nextconnect作为方法,我对所有这些都是全新的

我能够成功地创建一个帖子,并更新用户配置文件,但我完全无法删除帖子。我试图模仿我创建帖子的方式,但将其切换到
.deleteOne
而不是
.insertOne
。我还可以显示
post.\u id
以便我知道我可以访问它。我只是对如何将其传递到delete函数感到困惑

我知道我应该传入
post.\u id
,然后将其发送给
处理程序。delete
位于NextJS的
api/
文件夹中,然后调用
处理程序中的delete函数。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 });
});


回答

问题是我的变量名

// 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!");
    }
  };

我试过你的解决办法,但没用。我在处理程序函数中添加了控制台日志,单击该按钮将打印控制台日志,但没有执行任何其他操作。很遗憾,如果您的项目不是私有项目,请发送一个沙盒或指向repo的链接,以便我可以检查它。我发现了问题:)。我将在下面添加一个答案以供参考。
// 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 });
});
// 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);
}