Bird
0
0

You want to implement a NestJS service method that updates a resource only if it exists, otherwise throws a NotFoundException. Which approach is correct?

hard📝 Conceptual Q8 of 15
NestJS - Database with TypeORM
You want to implement a NestJS service method that updates a resource only if it exists, otherwise throws a NotFoundException. Which approach is correct?
AFind the resource first; if not found, throw NotFoundException; else update and return
BDirectly call update and catch errors if resource missing
CAlways create a new resource if update fails
DReturn null if resource not found without exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand update with existence check

    Best practice is to find resource first, throw NotFoundException if missing.
  2. Step 2: Update only if resource exists

    After confirming existence, update and return updated resource.
  3. Final Answer:

    Find the resource first; if not found, throw NotFoundException; else update and return -> Option A
  4. Quick Check:

    Check existence before update to handle errors [OK]
Quick Trick: Check existence before update to throw NotFoundException [OK]
Common Mistakes:
  • Ignoring existence check
  • Catching errors instead of checking
  • Returning null silently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes