I currently have a dynamic API route file specifically designed for handling POST requests. Below is the code snippet of this file:
import { NextRequest, NextResponse } from "next/server";
import dbConnect from "@/lib/dbConnect";
import User from "@/models/User";
export async function POST(req: NextRequest, res: NextResponse) {
const data = await req.json();
const { name, email, password, country } = data;
await dbConnect();
try {
const user = await User.create({
name, email, password, country
})
return NextResponse.json({
success: true,
data: user,
}, {
status: 201,
})
} catch (error) {
return NextResponse.json({
success: false,
}, {
status: 400,
})
}
}
Upon attempting to substitute NextResponse
with res
within the route, an error occurs. Here's the relevant section:
return res.json({
success: true,
data: user,
}, {
status: 201,
})
The error message reads as follows:
Expected 0 arguments, but got 2.ts(2554)
.
https://i.sstatic.net/z3bru.png
What could be the reason behind such errors when attempting to replace NextResponse
with res
?