NEXT JS req.body 未定义

Keo*_*Dev 4 javascript fetch reactjs next.js

不确定我做错了什么?

当我尝试将表单数据上传到我的 SUPABASE 时,数据未定义,数据传入 API 时未定义,但是当我将其传递给函数时,它会在提交处理程序中打印我想要发送到 API 的内容。

export const Quote = () => {
  const [formIsValid, setFormIsValid] = useState(false);
  //----------------------------_FORM VALIDATION------------------------------------
  const {
    value: firstName,
    inputBlurChangeHandler: firstNameBlur,
    isValid: firstNameValid,
    hasError: firstNameInputHasError,
    valueChangeHandler: firstNameChangeHandler,
    reset: resetFirstName,
  } = useInput((value) => value.trim() !== "");



**hooks & useEffect removed to shorten question they are same as above but different names**

  console.log(formIsValid, "FORM IS VALID");


  const formSubmitHandler = async (event) => {
    event.preventDefault();

    //UNDEFINEDS
    await fetch("api/dbhandler", {
      method: "POST",
      body: {
        firstname: firstName,
        secondname: secondName,
        street: streetAddress,
        phone: phoneNumber,
        email: emailAddress,
        postal: postalCode,
        about: quoteDescription,
      },
      headers: {
        "Content-Type": `text/plain`,
      },
    });
  };
Run Code Online (Sandbox Code Playgroud)

API 在 req 中未定义。body 但如果我控制台登录提交处理程序值将被传递给函数,不确定我做错了什么

import { supabase } from "../../utils/supabaseClient";

const supabaseApiHandler = async (req, res) => {
  console.log(req.body.firstname);
  if (req.method === "POST") {
    const firstname = req.body.firstname;
    const secondname = req.body.secondname;
    const email = req.body.email;
    const street = req.body.street;
    const postal = req.body.postal;
    const phone = req.body.phone;
    const about = req.body.about;

    const { data, error } = await supabase.from("quotes").insert([
      {
        firstname,
        secondname,
        email,
        street,
        postal,
        phone,
        about,
      },
    ]);
  }

  res.status(200).json({ name: "test" });
};

export default supabaseApiHandler;
Run Code Online (Sandbox Code Playgroud)

use*_*459 16

如果您在 API 路由中禁用了正文解析器,则 req.body 将为空。

我不小心留下了这段代码,而没有使用另一个主体解析器。

export const config = {
  api: {
    bodyParser: false,
  },
};
Run Code Online (Sandbox Code Playgroud)