Using an Angular 2 http.post without subscribing? Or am I thinking of it wrong?

Methodician picture Methodician · Jul 29, 2016 · Viewed 7.1k times · Source

Is there a way to just tell the server to update data without subscribing? Skipping a return statement and a subscription seems to render the http call inert.

In my case, my DB guy created a bunch of stored procedures that return nothing and sometimes I want to do something simple like this in my service:

public setItem(item: IRequestItem) {
        this.http.post('api/items', item);
    }

and call it like this in my component:

save() {
        var comp = this;
        this.items.forEach(function(item) {
            comp.service.setItem(item)
        });
    }

Instead I have to do something like this in the service:

public setItem(item: IRequestItem) {
        return this.http.post('api/items', item);
    }

And then call it like this in the conponent:

save() {
        var comp = this;
        this.items.forEach(function(item) {
            comp.service.setItem(item).subscribe(r => console.log(r));
        });
    }

Which would return lots of these:

Response {_body: "", status: 204, ok: true, statusText: "No Content", headers: Headers…}
_body : ""
headers : Headers
ok : true
status : 204
statusText : "No Content"
type : 2
url : "http://localhost:56018/api/items"
__proto__ : Object

I'm just learning so maybe I'm looking at it wrong. Can I interpret something in that Response object that will let me know if the operation failed or succeeded? Or is there another syntax that will just return a success or failure instead of the confusing "No Content" response?

Answer

Aluan Haddad picture Aluan Haddad · Mar 8, 2017

In order to trigger the request, you need to call subscribe but you do not need to return the request observable from the service and call it from the component, you can call subscribe from within the service.

setItem(item: RequestItem) {
  this.http.post('api/items', item)
    .subscribe({ error: e => console.error(e) });
}

Now in the above, the service will immediately return, before the request completes but the request will be made and any errors logged from within the service.