首页  >  问答  >  正文

Next.js中使用DELETE方法时,req.body未定义

我真的不知道为什么,但是当我尝试获取数据并将其放入响应正文中时,它显示未定义(在控制台中)。我有两个几乎相同的组件。一种使用 POST 方法并返回填充的正文,另一种使用 DELETE 方法并返回未定义的正文。我正在使用 Prisma 架构。

这是可以运行并返回 API 正文的 POST

export default function Product({
  id_product,
  name,
  link_image,
  price,
}: ProductProps) {
  const [test, testing] = useState(false);
  const { push: relocate } = useRouter();

  const onAddToCart = async () => {

    let response = await fetch("/api/addToCart", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        id_product: id_product,
      }),
    });

    if (response.ok) {
      toast.success(`${name} was added to the cart`);
    } else {
      toast.error(`${name} is already in your cart`);
    }
  };

这是该函数的 API 的开始,const { id_product } = req.body 有效。

async function handlePost(req: NextApiRequest, res: NextApiResponse) {
    const session = await getServerSession(req, res, authOptions);
    const client = connexion()
    const { id_product } = req.body;
 
    const user = await client.user.findFirst({
        where: { email: session?.user?.email || undefined}
    })

    let cart = await client.cart.findFirst({
        where: {id_user: user?.id_user}
    })

这就是我遇到的问题,除了方法之外,组件基本相同:

type ProductProps = products;

export default function ProductItem({
  id_product,
  description,
  publication_date,
  author,
  name,
  link_image,
  price,
}: ProductProps) {
  const onDeleteFromCart = async () => {
    let data = {
      id_product: id_product
    }
    let response = await fetch("/api/deleteFromCart", {
      method: "DELETE",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(data),
    });
    if (response.ok) {
        toast.success(`${name} was succesfully removed from your cart`)
    }
    else {
        toast.error(`Error`);
      }
  };

这是 API,const {id_product} = req.body 未定义

async function handleDelete(req: NextApiRequest, res: NextApiResponse) {
    const session = await getServerSession(req, res, authOptions);
    const client = connexion()
    const  { id_product } = req.body
console.log(id_product)
    const user = await client.user.findFirst({
        where: { email: session?.user?.email || undefined}
    });
 
    let cart = await client.cart.findFirst({
        where: {id_user: user?.id_user}
    });
    let cart_item = await client.cart_item.findFirst({
        where: {
            id_cart: cart?.id,
            id_product: id_product
        }
    })

我已经尝试解决这个问题几个小时了,但完全没有进展。

P粉652523980P粉652523980262 天前383

全部回复(2)我来回复

  • P粉477369269

    P粉4773692692024-01-03 20:14:48

    这在最近更新之前一直有效。 GIthub 上有很多问题,但我不知道 Next.js 的维护者是否已做出回应。目前它阻止我们更新。我知道这并不典型,但这是 Next.js 的一项重大更改,我不想迁移所有 DELETE 端点:(。

    https://github.com/vercel/next.js/issues/49353

    https://github.com/vercel/next.js/issues/48096

    https://github.com/vercel/next.js/issues/48898

    回复
    0
  • P粉609866533

    P粉6098665332024-01-03 18:43:13

    delete 请求不包含正文,如果您需要在此请求中包含正文,可以尝试使用 patch 方法

    回复
    0
  • 取消回复