Next.js 14 POST – TypeError: Body is unusable at specConsumeBody

I am trying to POST user details to an API route in Next.js 14. It works correctly when only sending the id and firstname, however I am receiving the error (below) once I start adding an additional field, for example “lastname”.

Working code:

export async function POST(
  req: Request,
) {
  try {
    const { getUser } = getKindeServerSession();
    const authuser = await getUser()

    const id = await authuser?.id
    const { firstname } = await req.json();
   
    if (!id) {
      return new NextResponse("Unauthorized", { status: 401 });
    }

    const user = await db.user.create({
      data: {
        id,
        firstname: firstname,        
       }
    });

   console.log(user);

    return NextResponse.json(user);
  } catch (error) {
    console.log("[USER]", error);
    return new NextResponse("Internal Error", { status: 500 });
  }
}


console.log(user);

{
  id: 'kp_7e90fc74577e46bfb3133eb73ac8ed89',
  firstname: 'Louis2',
  lastname: null,
  grade: null,
  profilepicture: null,
  email: null,
  contactnumber: null,
  isActive: false,
  createdAt: 2024-04-16T19:01:21.871Z,
  updatedAt: 2024-04-16T19:01:21.871Z
}

Code not working (added lastname):

export async function POST(
  req: Request,
) {
  try {
    const { getUser } = getKindeServerSession();
    const authuser = await getUser()

    const id = await authuser?.id
    const { firstname } = await req.json();
    const { lastname } = await req.json();
   
    if (!id) {
      return new NextResponse("Unauthorized", { status: 401 });
    }

    const user = await db.user.create({
      data: {
        id,
        firstname: firstname,  
        lastname: lastname,
       }
    });

   console.log(user);

    return NextResponse.json(user);
  } catch (error) {
    console.log("[USER]", error);
    return new NextResponse("Internal Error", { status: 500 });
  }
}

Error message:

[USER] TypeError: Body is unusable
    at specConsumeBody (node:internal/deps/undici/undici:5294:15)
    at tl.json (node:internal/deps/undici/undici:5196:18)
    at c (/var/task/.next/server/app/api/user/route.js:1:1075)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:42484
    at async eI.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:32486)
    at async eI.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:43737)
    at async Y (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:24659)
    at async Q.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1025)
    at async r2.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:507)