code: 'P2010', clientVersion: '5.10.2', meta: { modelName: 'User', code: 'unknown', message: 'Kind: Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Set Name: atlas-oxeoqy-shard-0, Servers: [ { Address: ac-ezmsl0b-shard-00-00.yiwojeu.mongodb.net:27017, Type: Unknown, Error: Kind: I/O error: received fatal alert: InternalError, labels: {} }, { Address: ac-ezmsl0b-shard-00-02.yiwojeu.mongodb.net:27017, Type: Unknown, Error: Kind: I/O error: received fatal alert: InternalError, labels: {} }, { Address: ac-ezmsl0b-shard-00-01.yiwojeu.mongodb.net:27017, Type: Unknown, Error: Kind: I/O error: received fatal alert: InternalError, labels: {} } ] }, labels: {}' } }
route.ts
import prisma from "@/utils/connect";
export const GET = async () => {
try {
// const prisma = new PrismaClient();
const categories = await prisma.category.findMany();
return new NextResponse(JSON.stringify(categories), { status: 200 });
} catch (err) {
console.log(err);
return new NextResponse(
JSON.stringify({ message: "Something went wrong" }),
{ status: 500 }
);
}
};
connect.ts
import { PrismaClient } from "@prisma/client";
const prismaClientSingleton = () => {
return new PrismaClient();
};
declare global {
var prisma: undefined | ReturnType<typeof prismaClientSingleton>;
}
const prisma = globalThis.prisma ?? prismaClientSingleton();
export default prisma;
if (process.env.NODE_ENV !== "production") globalThis.prisma = prisma;
Created model in schema.prisma
model Category {
id String @id @default(cuid()) @map("_id")
slug String @unique
title String
img String?
Posts Post[]
}
On http://localhost:3000/api/categories => {"message":"Something went wrong"}
Getting error on
const categories = await prisma.category.findMany();