Bird
0
0

Identify the error in this resolver code:

medium📝 Debug Q14 of 15
Angular - Advanced Patterns
Identify the error in this resolver code:
export class ProductResolver implements Resolve<Product> {
  constructor(private productService: ProductService) {}
  resolve(route: ActivatedRouteSnapshot): Product {
    const id = route.paramMap.get('id')!;
    this.productService.getProduct(id).subscribe(product => {
      return product;
    });
  }
}
AThe resolve method returns void instead of Product or Observable<Product>
BThe subscribe method is missing a callback function
CThe constructor is missing dependency injection
DThe route parameter 'id' is not accessed correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check resolve method return type

    The resolve method declares it returns Product but actually returns nothing because subscribe is asynchronous and returns void.
  2. Step 2: Understand correct return for resolver

    Resolvers must return Observable or Promise, not void. Using subscribe inside resolve breaks this contract.
  3. Final Answer:

    The resolve method returns void instead of Product or Observable -> Option A
  4. Quick Check:

    Resolvers must return Observable or Promise, not void [OK]
Quick Trick: Never subscribe inside resolve; return Observable or Promise directly [OK]
Common Mistakes:
  • Using subscribe inside resolve instead of returning Observable
  • Returning wrong data type from resolve
  • Ignoring asynchronous nature of data fetching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes