Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Angular - HTTP Client
Identify the error in this code snippet:
const params = new HttpParams();
params.set('id', '123');
this.http.get('/users', { params }).subscribe();
AHttpParams is immutable; set() must be assigned back
BHttpParams cannot be used with get()
Cset() method does not exist on HttpParams
DMissing headers object
Step-by-Step Solution
Solution:
  1. Step 1: Recognize HttpParams immutability

    HttpParams methods like set() return a new instance; they do not modify the original.
  2. Step 2: Identify missing assignment

    The code calls params.set() but does not assign the result back, so params remains empty.
  3. Final Answer:

    HttpParams is immutable; set() must be assigned back -> Option A
  4. Quick Check:

    Immutable params require reassignment [OK]
Quick Trick: Always assign set() result back to params variable [OK]
Common Mistakes:
MISTAKES
  • Ignoring immutability of HttpParams
  • Expecting set() to mutate original object
  • Confusing HttpParams with mutable objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes