Bird
0
0

Identify the error in this NestJS repository injection code snippet:

medium📝 Debug Q14 of 15
NestJS - Database with TypeORM
Identify the error in this NestJS repository injection code snippet:
@Injectable()
export class ProductService {
  constructor(private productRepository: Repository) {}
}
AMissing @InjectRepository decorator for productRepository
BRepository should be injected using @InjectableRepository
CConstructor should not have parameters
DRepository cannot be injected in services
Step-by-Step Solution
Solution:
  1. Step 1: Check how repositories are injected in NestJS

    Repositories must be injected with @InjectRepository(Entity) decorator to work properly.
  2. Step 2: Identify missing decorator in the code

    The code misses @InjectRepository(Product) before the repository parameter.
  3. Final Answer:

    Missing @InjectRepository decorator for productRepository -> Option A
  4. Quick Check:

    Repository injection needs @InjectRepository [OK]
Quick Trick: Always use @InjectRepository for repositories in constructor [OK]
Common Mistakes:
  • Forgetting @InjectRepository decorator
  • Using wrong decorator names
  • Assuming repositories inject automatically without decorators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes