I am encountering an issue with my DELETE mapping
export async function DELETE({params} : {params: {id: string}}) {
try {
const loanToDelete = await prisma.loan.findUnique({
where: { id: parseInt(params.id) }
})
if (!loanToDelete) {
return NextResponse.json({message: "Loan not found"}, {status: 404})
}
await prisma.loan.delete({
where: {id: loanToDelete.id}
})
return NextResponse.json({message: "Loan successfully deleted"}, {status: 200})
} catch (error) {
console.error(error)
return NextResponse.json({message: "An error occurred"}, {status: 500})
}
}
It appears that the params are not being read correctly in this DELETE request.
When testing this endpoint in Postman with
http://localhost:3000/api/loans/27
, I receive the following error:
TypeError: Cannot read properties of undefined (reading 'id')
This is puzzling to me as my GET mapping, which is similar except for returning an object, works fine in reading the id from params.
export async function GET(response: NextResponse, {params} : {params: {id: string}}) {
try {
const loan = await prisma.loan.findUnique({
where: {id: parseInt(params.id)}
})
if (!loan) {
return NextResponse.json({message: "Loan not found"}, {status: 404})
}
return NextResponse.json(loan, {status: 200})
}
catch {
return NextResponse.json({message: "An error occurred"}, {status: 500})
}
}
Is Next.js handling DELETE requests differently when it comes to reading params compared to GET requests?