Steven
Steven

Reputation: 1256

Angular HttpClient call ignored

So, I have a Jhipster generated app.

In it we have a number of entities with "default" methods both front and back end

One of these methods is the create, in this case for entity ActivePharmaIng, this is what the code looks like in the angular service class:

create(activePharmaIng: ActivePharmaIng): Observable<ActivePharmaIng> {
  const copy = this.convert(activePharmaIng);
  return this.http.post(this.resourceUrl, copy).map((res: Response) => {
    const jsonResponse = res.json();
    return this.convertItemFromServer(jsonResponse);
  });
}

So, this code works like a charm where it's getting used in the Entities area, however, when I try to reuse it in a modal I call it from a function which is called with (click), the code for the function:

saveNewApi(api: ActivePharmaIng) {
  this.activePharmaIngDTO.api = this.activePharmaIng;
  this.activePharmaIngService.create(api);
  this.show = false;
}

In the second line we can see the call to create, when debugging I see the function gets called seamlessly and It goes in to the create function only to just jump straight out as after trying to do the call

I have no trace of any error messages, not debugging with IntelliJ, not in the google developer toolbar, tried in console and Network area, not a single message. It just jumps to the next line, which is a boolean that when setting to false hides some divs of a form:

Upvotes: 2

Views: 2502

Answers (1)

Igor
Igor

Reputation: 62213

You have to subscribe to the observable if you want your call to fire. Also you should probably move the this.show = false; into the call back of the observable.

saveNewApi(api: ActivePharmaIng){
    this.activePharmaIngDTO.api = this.activePharmaIng;
    this.activePharmaIngService.create(api).subscribe(result => {
        this.show = false;
    });
}

See HttpClient documentation:

Note the subscribe() method. All Observables returned from HttpClient are cold, which is to say that they are blueprints for making requests. Nothing will happen until you call subscribe(), and every such call will make a separate request. For example, this code sends a POST request with the same data twice

Upvotes: 9

Related Questions