I am new to using NextJs and Typescript. I am making a route handler and I cannot figure out the return type of the below function. I thought NextApiResonse<userType | null> should work but it doesn’t. What am I doing wrong?
import { UserModel } from "@/lib/models";
import { connectMongoDb } from "@/lib/utils";
import { userType } from "@/types/types";
import { NextApiRequest, NextApiResponse } from "next";
import { NextResponse } from "next/server";
export const GET = async (request:NextApiRequest, { params }:{params:{id:"string"}}) => {
console.log("params",params)
const { id } = params;
try {
connectMongoDb();
const user:userType = await UserModel.findById(id);
if (user) {
return NextResponse.json(user);
} else {
return NextResponse.json({ error: "User not found" });
}
} catch (error) {
console.log("API getuser error", error);
return NextResponse.json({ error: "Failed to fetch user" });
throw new Error("Failed to fetch user");
}
};
If anyone can guide me and help me figure out the correct return type of the above function, I will be thankful.