I’m creating next.js project with mongodb.
I completed to deploy to vercel.
but i have a problem.
not working app/api/user/route.ts in deploy site.
it is displayed 404 not found.
but the data exist on mongodb and working on local.
I don’ know how to resolve it.
import { NextRequest, NextResponse } from "next/server";
import { connectDb } from "@/utils/database";
import { UserModel } from "@/models/userModel";
export const GET = async (request: NextRequest) => {
try {
await connectDb();
// get name from query
const { searchParams } = new URL(request.url);
const name = searchParams.get("name");
if (!name) {
return NextResponse.json(
{ message: "message" },
{ status: 400 }
);
}
const user = await UserModel.findOne({ name });
if (user) {
return NextResponse.json({
message: "successful",
user: user,
});
} else {
return NextResponse.json(
{ message: "failed" },
{ status: 404 }
);
}
} catch (error) {
return NextResponse.json(
{ message: "failed" },
{ status: 500 }
);
}
};
↑this is my app/api/user/route.ts
import mongoose from "mongoose";
export const connectDb = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI || "");
} catch (error) {
console.log("error");
throw new Error();
}
};
this is my utils/database.ts
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/user?name=${encodeURIComponent(
username
)}`,
{
method: "GET",
}
);
const data = await response.json();
if (response.ok) {
setUser(data.user);
} else {
setError(data.message);
}
} catch (error) {
setError("failed");
}
What is the problem…
(sorry… I’m a beginner for English.)