Bird
0
0

You wrote this PrismaService method but get a runtime error:

medium📝 Debug Q6 of 15
NestJS - Database with Prisma
You wrote this PrismaService method but get a runtime error:
async getUser(id: number) {
  return this.prisma.user.findUnique({ where: { id } });
}
What is the likely cause?
AThe id parameter type should be string, not number
BPrismaClient is not instantiated in the service
CfindUnique requires a unique field, id might not be unique
DMissing await before the findUnique call
Step-by-Step Solution
Solution:
  1. Step 1: Check PrismaClient instantiation

    If PrismaClient is not instantiated or injected, calling this.prisma.user.findUnique causes runtime error.
  2. Step 2: Validate other options

    Id is usually a number and unique; missing await causes Promise but not runtime error.
  3. Final Answer:

    PrismaClient is not instantiated in the service -> Option B
  4. Quick Check:

    Runtime error often means PrismaClient not instantiated [OK]
Quick Trick: Always instantiate PrismaClient before using it in service [OK]
Common Mistakes:
  • Assuming id must be string
  • Confusing missing await with runtime error
  • Ignoring PrismaClient initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes