bensiu
bensiu

Reputation: 25564

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?

Upvotes: 0

Views: 62

Answers (1)

ferhado
ferhado

Reputation: 2594

You have to use subscribe or toPromise

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

Upvotes: 1

Related Questions