Bird
0
0

This Angular component code snippet has a bug:

medium📝 Debug Q7 of 15
Angular - HTTP Client
This Angular component code snippet has a bug:
loading = false;
error = '';

fetchData() {
  this.loading = true;
  this.error = '';
  fetch('api/data')
    .then(() => {
      this.loading = false;
      this.error = 'No error';
    })
    .catch(() => {
      this.loading = false;
      this.error = 'Failed';
    });
}

What is the issue with setting error to 'No error' on success?
AIt causes a syntax error
BIt incorrectly shows an error message when there is none
CIt prevents loading from stopping
DIt causes fetch to fail
Step-by-Step Solution
Solution:
  1. Step 1: Understand error message usage

    Error message should be empty or null on success, not a string like 'No error'.
  2. Step 2: Identify UI impact

    Setting error to 'No error' will display this message, confusing users.
  3. Final Answer:

    It incorrectly shows an error message when there is none -> Option B
  4. Quick Check:

    Clear error on success, don't set misleading messages [OK]
Quick Trick: Clear error string on success to avoid false error display [OK]
Common Mistakes:
MISTAKES
  • Setting error to 'No error' string
  • Not clearing error on success
  • Confusing error with loading flag

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes