Next.js Discord

Discord Forum

Next 14 is returning TypeError: res.status is not a function

Answered
Korat posted this in #help-forum
Open in Discord
KoratOP
// users/signup/route.ts
export async function GET( // -- according to docs
  req: NextApiRequest,
  res: NextApiResponse<ResponseData>
) {
  console.log(req.method);
  res.status(200).json({ message: "Hello from Next.js!" });
}

export async function POST( // -- my custom controller 
  req: NextApiRequest,
  res: NextApiResponse<ResponseData>
) {
  const { username, email, password } = req.body;

  try {
    const userExist = await User.findOne({ email });
    if (userExist) throw new Error("Email is already being used");

    const newUser = new User({
      username,
      email,
      password,
    });

    await newUser.save();

    res.json({
      result: true,
      status: 201,
      data: newUser,
      message: "User has being successfully saved.",
    });
  } catch (err: any) {
    res.json({
      result: false,
      status: 500,
      message: err.message,
    });
  }
}
Answered by Cape horse mackerel
np, just mark a post as resolved 🙂
View full answer

11 Replies

Cape horse mackerel
you're probably looking at the "pages" documentation
instead of res.json() try to use NextResponse.json()
KoratOP
Let me try
@Cape horse mackerel you're probably looking at the "pages" documentation instead of `res.json()` try to use `NextResponse.json()`
KoratOP
Seems like NextResponse is working but why does the docs suggest different it says to use NextApiResponse ?
Cape horse mackerel
can you send a docs link
@Cape horse mackerel can you send a docs link
KoratOP
Okay I guess I was following wrong docs my bad
Cape horse mackerel
yeah, you're watching the docs for the pages router, not the app
Thanks man
Cape horse mackerel
np, just mark a post as resolved 🙂
Answer