NextApiRequest
Unanswered
Sloth bear posted this in #help-forum
Sloth bearOP
When i try to run npm run build i keep getting this error:
.next/types/app/api/review/route.ts:150:7
Type error: Type '{ tag: "POST"; param_position: "first"; param_type: NextApiRequest & Partial<NextRequest>; }' does not satisfy the constraint 'ParamCheck<NextRequest | Request>'.
Types of property 'param_type' are incompatible.
Type 'NextApiRequest & Partial<NextRequest>' is not assignable to type 'NextRequest | Request'.
Type 'NextApiRequest & Partial<NextRequest>' is not assignable to type 'NextRequest'.
Property 'geo' is optional in type 'NextApiRequest & Partial<NextRequest>' but required in type 'NextRequest'.
148 | Diff<
149 | ParamCheck<Request | NextRequest>,
150 | {
| ^
151 | tag: 'POST'
152 | param_position: 'first'
153 | param_type: FirstArg<MaybeField<TEntry, 'POST'>>12 Replies
Sloth bearOP
Here my code:
import connectMongo from "@/lib/mongodb";
import Reviews from "@/models/reviews";
import User from "@/models/user";
import { NextApiRequest } from "next";
import { NextResponse } from "next/server";
export async function POST(req: NextApiRequest) {
const body = await new NextResponse(req.body).json();
await connectMongo();
const { message, user: auth0Id, name, email, image } = body;
if (!message || !auth0Id) {
console.log("Missing fields", { message, auth0Id });
return NextResponse.json({
message: "Missing required fields: user, message",
});
}
try {
const user = await User.findOneAndUpdate(
{ auth0Id },
{ auth0Id, name, email, image },
{ new: true, upsert: true }
);
const newReview = new Reviews({
user: user._id,
message,
});
const savedReview = await newReview.save();
return NextResponse.json(savedReview, { status: 201 });
} catch (err) {
console.error("Error in saving review:", err);
return NextResponse.json({ message: "Error creating review", error: err });
}
}
export async function GET(req: NextApiRequest) {
await connectMongo();
try {
const reviews = await Reviews.find({}).populate("user", "email name image");
return NextResponse.json(reviews, { status: 200 });
} catch (err) {
console.error("Error retrieving reviews:", err);
return NextResponse.json({
message: "Failed to retrieve reviews",
error: err,
});
}
}Sloth bearOP
Okay i changed my code to:
export const POST = async (req: NextRequest) => {
const { message, user: auth0Id, name, email, image } = await req.json();
await connectMongo();
if (!message || !auth0Id) {
console.log("Missing fields", { message, auth0Id });
return new NextResponse(JSON.stringify({
message: "Missing required fields: user, message",
}), { status: 400 });
}
try {
const user = await User.findOneAndUpdate(
{ auth0Id },
{ auth0Id, name, email, image },
{ new: true, upsert: true }
);
const newReview = new Reviews({
user: user._id,
message,
});
const savedReview = await newReview.save();
return new NextResponse(JSON.stringify(savedReview), { status: 201 });
} catch (err) {
console.error("Error in saving review:", err);
return new NextResponse(JSON.stringify({
message: "Error creating review", error: err
}), { status: 500 });
}
};It works fine when i run npm run dev. But when i run npm run build i get this error same error:
.next/types/app/api/user/route.ts:150:7
Type error: Type '{ __tag__: "POST"; __param_position__: "first"; __param_type__: NextApiRequest; }' does not satisfy the constraint 'ParamCheck<NextRequest | Request>'.
Types of property '__param_type__' are incompatible.
Type 'NextApiRequest' is not assignable to type 'NextRequest | Request'.
148 | Diff<
149 | ParamCheck<Request | NextRequest>,
> 150 | {
| ^
151 | __tag__: 'POST'
152 | __param_position__: 'first'
153 | __param_type__: FirstArg<MaybeField<TEntry, 'POST'>>
PS E:\main-projects\Portfolio\portfolio>@Ray oh i found the error i'm stupid... i been coding for 15hours hehe thanks for the help
Sloth bearOP
@Ray If I want to create a delete method, should I create a new file named api/review/[id].ts? and use the delete method within it? I have a review page where a user can log in using Auth0. After logging in, the user can write a review. Once the review is submitted, I want only the author of the review to have the ability to delete their own post. Other users should not see the delete button for reviews that aren't theirs. How would you implement this feature?
The file name of route handler have to be route.ts
I would use server action and check for the authentication before deleting
@Ray I would use server action and check for the authentication before deleting
Sloth bearOP
Ok thank you Ray!