Bird
0
0

What will be the output of this interceptor code snippet when the controller returns { data: 5 }?

medium📝 component behavior Q4 of 15
NestJS - Interceptors
What will be the output of this interceptor code snippet when the controller returns { data: 5 }?
intercept(context, next) {
  return next.handle().pipe(map(data => ({ ...data, extra: 10 })));
}
AError: map is not a function
B{ data: 5, extra: 10 }
C{ extra: 10 }
D{ data: 5 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the interceptor modifies response

    The interceptor uses map to add an extra property to the original data object.
  2. Step 2: Apply the map transformation

    The original { data: 5 } is spread and extra: 10 is added, resulting in { data: 5, extra: 10 }.
  3. Final Answer:

    { data: 5, extra: 10 } -> Option B
  4. Quick Check:

    map adds extra property = { data: 5, extra: 10 } [OK]
Quick Trick: map merges new properties into response object [OK]
Common Mistakes:
  • Expecting original data unchanged
  • Thinking map replaces entire object
  • Confusing map with filter or reduce

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes