Search code examples
angularhttpclient

Angular HttpClient not executing with post


I have a service class with an Angular application that looks like this:

@Injectable()
export class AbstractStore {
  protected url: string;

  constructor(protected http: HttpClient) {}

  read(url: string): Observable<any> {
    return this.http.get<any>(URL + url).pipe(
      ...
    );
  }

  loadRecords(): Observable<Record[]> {
    return this.read(this.url);
  }

  loadRecord(id: string): Observable<Record> {
    return this.read(`${this.url}/${id}`);
  }

  saveRecord(record: Record): Observable<Record> {
    // console statement executes
    console.log("Saving record..................", URL + this.url, record);

    // THIS NOT EXECUTE
    return this.http.post<any>(URL + this.url, record).pipe(
      ...
    );
  }

  handleError(error) {
    console.log(error.message || error);
  }
}

When calling read (http.get) methods it executes properly and I see traffic in the Network tab. When coming to saveRecord() method - it executes up to console statement. I do not see execution in the Network tab.

I not sure what I am doing wrong?


Solution

  • You have to use subscribe or toPromise

    return this.http.post<any>(URL + this.url, record)
      .pipe(
        ...
      ).toPromise();