Type error
Answered
Kromfohrländer posted this in #help-forum
KromfohrländerOP
Failed to compile.
.next/types/app/api/dashboard/admin/classes/[id]/students/[studentId]/route.ts:244:7
Type error: Type '{ __tag__: "DELETE"; __param_position__: "second"; __param_type__: { params: { id: string; studentId: string; }; }; }' does not satisfy the constraint 'ParamCheck<RouteContext>'.
The types of '__param_type__.params' are incompatible between these types.
Type '{ id: string; studentId: string; }' is missing the following properties from type 'Promise<any>': then, catch, finally, [Symbol.toStringTag]
242 | Diff<
243 | ParamCheck<RouteContext>,
> 244 | {
| ^
245 | __tag__: 'DELETE'
246 | __param_position__: 'second'
247 | __param_type__: SecondArg<MaybeField<TEntry, 'DELETE'>>
Answered by Asian black bear
The params are a promise in Next 15:
- { params }: { params: { id: string; studentId: string } }
+ { params }: { params: Promise<{ id: string; studentId: string }> }
3 Replies
KromfohrländerOP
src\app\api\dashboard\admin\classes[id]\students[studentId]\route.ts
import { NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { connectToDb } from '@/lib/mongodb'
import Class from '@/models/Class'
export async function DELETE(
request: Request,
{ params }: { params: { id: string; studentId: string } }
) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.role === 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { id, studentId } = params
await connectToDb()
const updatedClass = await Class.findByIdAndUpdate(
id,
{ $pull: { studentIds: studentId } },
{ new: true }
).populate('studentIds', 'name email')
return NextResponse.json(updatedClass)
} catch (error) {
return NextResponse.json({ error: 'Server error' }, { status: 500 })
}
}
Asian black bear
The params are a promise in Next 15:
- { params }: { params: { id: string; studentId: string } }
+ { params }: { params: Promise<{ id: string; studentId: string }> }
Answer