[Fixed] Error in Observable<> when creating service in Angular

Issue

I’m getting the following errors when creating the customer.service:

Type ‘boolean’ is not assignable to type ‘Observable<Customer[]>’.

‘GetResponses’ only refers to a type, but is being used as a value
here.

‘:’ expected.

Property ‘pipe’ does not exist on type ‘{ this: any; }’.

Property ‘_embedded’ does not exist on type ‘unknown’.

This is the code:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Customer } from '../common/customer';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
 
@Injectable({
  providedIn: 'root'
})
export class CustomerService {
 
  private baseUrl = 'http://localhost:8080/api/customerDetails';
 
  constructor(private httpClient: HttpClient) { }
 
  getCustomerList(): Observable<Customer[]> {
    return this.httpClient.get<GetResponses>{this.baseUrl}.pipe(
      map(response => response._embedded.customers)
    )
  }
}
 
interface GetResponses{
  _embedded : {
    customers: Customer[];
  }
}

It uses the following class:

export class Customer {
 
    mobileno: string;
    email: string;
    mobileImei: string;
    name: string;
    sdoCode: string;
    agency: string;
    profileImage: string;
    dateCreated: Date;
    dateUpdated: Date;
}

Could anyone let me know where I’m possibly going wrong?

Thanks in advance!

Solution

You simply used the wrong set of brackets here:

 get<GetResponses>{this.baseUrl}

Try it this way:

 getCustomerList(): Observable<Customer[]> {
    return this.httpClient.get<GetResponses>(this.baseUrl).pipe(
        map(response => response._embedded.customers)
    );
 }

Leave a Reply

(*) Required, Your email will not be published