Next 14 is returning TypeError: res.status is not a function
Answered
Korat posted this in #help-forum
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,
});
}
}11 Replies
Cape horse mackerel
you're probably looking at the "pages" documentation
instead of
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
https://nextjs.org/docs/app/building-your-application/routing
this are the right one
this are the right one
KoratOP
Thanks man
Cape horse mackerel
np, just mark a post as resolved 🙂
Answer