findMany() vs findMany({...})

// src/lib/db/db.ts
import { PrismaClient } from '@prisma/client';

const globalForPrisma = global as unknown as { prisma?: PrismaClient };

export const prisma = globalForPrisma.prisma ?? new PrismaClient();

if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}

async function listCounties() {
try {
console.log("DATABASE_URL:", process.env.DATABASE_URL);

const counties = await prisma.county.findMany();
console.log("Counties:", counties);
} catch (error) {
console.error("Error listing counties:", error);
}
}

void listCounties();
// src/lib/db/db.ts
import { PrismaClient } from '@prisma/client';

const globalForPrisma = global as unknown as { prisma?: PrismaClient };

export const prisma = globalForPrisma.prisma ?? new PrismaClient();

if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}

async function listCounties() {
try {
console.log("DATABASE_URL:", process.env.DATABASE_URL);

const counties = await prisma.county.findMany();
console.log("Counties:", counties);
} catch (error) {
console.error("Error listing counties:", error);
}
}

void listCounties();
//src/lib/db/list.ts
export async function getUKCounties(): Promise<County[]> {
console.log('Fetching UK counties...');
try {
const counties = await prisma.county.findMany();
console.log('FindMany result:', counties);
return counties;
} catch (error) {
console.error('Error fetching UK counties:', error);
throw error;
}
}
//src/lib/db/list.ts
export async function getUKCounties(): Promise<County[]> {
console.log('Fetching UK counties...');
try {
const counties = await prisma.county.findMany();
console.log('FindMany result:', counties);
return counties;
} catch (error) {
console.error('Error fetching UK counties:', error);
throw error;
}
}
In the db.ts its nicely returns the counties as expected. However in the lists.ts its empty. I've tried lot of versions and directy query or if i pass {where, selecet..} details to findMany, then it works in the getUKCounties too. So these works well:
const counties = await prisma.$queryRaw<County[]>`SELECT * FROM "County"`;
const counties = await prisma.county.findMany({
where: {},
select: {
id: true,
name: true
}
});
const counties = await prisma.$queryRaw<County[]>`SELECT * FROM "County"`;
const counties = await prisma.county.findMany({
where: {},
select: {
id: true,
name: true
}
});
I just started to use prisma, and while I found solution, I would be curious why in db.ts works simpty findMany, but not in the lists.ts
3 Replies
Prisma AI Help
You decided to hold for human wisdom. We'll chime in soon! Meanwhile, #ask-ai is there if you need a quick second opinion.
Nurul
Nurul4d ago
Do you get any errors in list.ts file? How are you invoking getUKCounties in list.ts file?
Acidias
AcidiasOP4d ago
No errors. Just an empty [] in the db.ts
const counties = await prisma.county.findMany();
console.log("Counties:", counties);
const counties = await prisma.county.findMany();
console.log("Counties:", counties);
log: Counties: [ { id: 1, name: 'test' }, { id: 2, name: 'Test' } ] But in lists.ts
const counties = await prisma.county.findMany();
console.log('FindMany result:', counties);
const counties = await prisma.county.findMany();
console.log('FindMany result:', counties);
log: FindMany result: [] This is where I call the lists.ts
// src/app/api/sign-up-lists/route.ts

import { NextResponse } from 'next/server';
import {
getUKCounties,
getWritingExperienceLevels,
} from '@/lib/db/lists';
import type { County, WritingExperienceLevel } from '@prisma/client';

interface OptionsResponse {
ukCounties?: County[];
writingExperienceLevels?: WritingExperienceLevel[];
error?: string;
}

export async function GET(): Promise<NextResponse<OptionsResponse>> {
try {
const ukCounties = await getUKCounties();
const writingExperienceLevels = await getWritingExperienceLevels();
return NextResponse.json({ ukCounties, writingExperienceLevels });
} catch (error) {
console.error(error);
return NextResponse.json({ error: 'Failed to load data' }, { status: 500 });
}
}
// src/app/api/sign-up-lists/route.ts

import { NextResponse } from 'next/server';
import {
getUKCounties,
getWritingExperienceLevels,
} from '@/lib/db/lists';
import type { County, WritingExperienceLevel } from '@prisma/client';

interface OptionsResponse {
ukCounties?: County[];
writingExperienceLevels?: WritingExperienceLevel[];
error?: string;
}

export async function GET(): Promise<NextResponse<OptionsResponse>> {
try {
const ukCounties = await getUKCounties();
const writingExperienceLevels = await getWritingExperienceLevels();
return NextResponse.json({ ukCounties, writingExperienceLevels });
} catch (error) {
console.error(error);
return NextResponse.json({ error: 'Failed to load data' }, { status: 500 });
}
}
But I when I change the getUKCounties to get the counties like:
const counties = await prisma.county.findMany({
where: {},
select: {
id: true,
name: true
}
});
const counties = await prisma.county.findMany({
where: {},
select: {
id: true,
name: true
}
});
Its works just fine

Did you find this page helpful?