类型错误:post._id 未定义

问题描述 投票:0回答:1

我正在学习NextJS,遇到以下错误:

const QuoteCard = ({ post }) => {
  const handleEdit = (post) => {
    router.push(`/update-quote/${post._id.toString()}`);
  };

  const handleDelete = async (post) => {
    const hasConfirmed = confirm(
      "Are you sure you want to delete this quote?"
    );

    if (hasConfirmed) {
      try {
        await fetch(`/api/quote/${post._id.toString()}`, {
          method: "DELETE",
        });

        const filteredPosts = myPosts.filter((item) => item._id !== post._id);

        setMyPosts(filteredPosts);
      } catch (error) {
        console.log(error);
      }
    }
  };
};

每当我尝试调用handleEdit或handleDelete函数时,我都会收到一条错误消息,指出post._id未定义。然而,涉及“发布”的所有其他功能都工作正常。

我尝试将日志记录到控制台,它确实有一个“_id”属性,问题可能是什么?

javascript reactjs node.js next.js web-development-server
1个回答
0
投票

很明显,从外门传过来的

post
的值没有“_id”字段。

© www.soinside.com 2019 - 2024. All rights reserved.