Skip to content

Commit 466b7d0

Browse files
fix:model issue on mongodb
1 parent e73a359 commit 466b7d0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/app/api/search/route.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,34 @@ export async function GET(req: Request) {
99
await connectToDatabase();
1010
const url = new URL(req.url);
1111
const searchText = url.searchParams.get("text");
12+
console.log(searchText);
1213

1314
if (!searchText) {
1415
return NextResponse.json(
1516
{ message: "Text query parameter is required" },
16-
{ status: 400 }
17+
{ status: 400 },
1718
);
1819
}
1920

2021
const subjects = await Paper.aggregate([
21-
{ $match: { subject: { $regex: searchText, $options: "i" } } },
22+
{ $match: { subject: { $regex: new RegExp(searchText, "i") } } }, // case-insensitive partial match
2223
{ $group: { _id: "$subject" } },
2324
{ $project: { _id: 0, subject: "$_id" } },
2425
]);
26+
console.log(subjects);
2527

2628
if (subjects.length === 0) {
2729
return NextResponse.json(
2830
{ message: "No subjects found for the specified text" },
29-
{ status: 404 }
31+
{ status: 404 },
3032
);
3133
}
3234

3335
return NextResponse.json({ subjects }, { status: 200 });
3436
} catch (error) {
3537
return NextResponse.json(
3638
{ message: "Failed to fetch subjects", error },
37-
{ status: 500 }
39+
{ status: 500 },
3840
);
3941
}
4042
}

src/db/papers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import mongoose, { Schema, type Model } from "mongoose";
22
import { type IPaper } from "@/interface";
33

44
const paperSchema = new Schema<IPaper>({
5-
public_id_cloudinary: {type: String, required: true},
5+
public_id_cloudinary: { type: String, required: true },
66
finalUrl: { type: String, required: true },
77
thumbnailUrl: { type: String, required: true },
88
subject: { type: String, required: true, index: true },
@@ -12,9 +12,7 @@ const paperSchema = new Schema<IPaper>({
1212
isSelected: { type: Boolean, default: false },
1313
});
1414

15-
paperSchema.index({ subject: 1 });
16-
1715
const Paper: Model<IPaper> =
18-
mongoose.models.Admin ?? mongoose.model<IPaper>("Admin", paperSchema);
16+
mongoose.models.paper ?? mongoose.model<IPaper>("paper", paperSchema);
1917

2018
export default Paper;

0 commit comments

Comments
 (0)