Update API Request - Next.js Course

Hi Mosh,

In your Next.js Course, Building APIs - 5: Updating an Object I noticed that we’re not actually updating anything. I was expecting to update the name which we set in /users here like this for example: return NextResponse.json({ id: params.id, name: body.name });.

Also, if you could add a category for Next.js please on this forum that’ll be hand.

Great course so far so thanks for putting this together!

export const PUT = async (request: NextRequest, { params }: { params: { id: number } }) => {
  // Validate the request body. If invalid, return 400 error
  const body = await request.json();
  if (!body.name) {
    return NextResponse.json({ error: 'Name is required' }, { status: 400 });
  }

  // If the ID doesn't exist, return 404 error
  if (params.id > 10) {
    return NextResponse.json({ error: 'User not found' }, { status: 400 });
  }

  // Otherwise fetch the user with the given ID and update the name with the name in the response body
  return NextResponse.json({ id: params.id, name: body.name });

  // Otherwise update the user and return
  // return NextResponse.json({ id: 1, name: body.name });
}