Skip to content

Commit

Permalink
fix : Cannot convert undefined null to object
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Jul 3, 2024
1 parent 88d71fb commit ecab573
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/api/city/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dbConfig from "@utils/db";
import StateDocument from "@models/citystate_hospitals";

export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
Expand All @@ -8,20 +9,19 @@ export async function GET(req: Request) {
return new Response("State parameter is missing", { status: 400 });
}

const db = await dbConfig();
const collection = db.collection("citystate_hospitals");

const stateHospitals = await collection.findOne({
await dbConfig();
const stateDocument = await StateDocument.findOne({
[state]: { $exists: true },
});

if (!stateHospitals) {
if (!stateDocument) {
return new Response("State not found", { status: 404 });
}

const cities: string[] = Object.keys(stateHospitals[state]);
// Get the cities for the given state
const cities = Object.keys(stateDocument.get(state));

if (!cities) {
if (cities.length === 0) {
return Response.json(cities, { status: 404 });
}

Expand Down

0 comments on commit ecab573

Please sign in to comment.