Search code examples
angularxmlhttprequest

Angular: How to make get request after post/put request is successful?


I got device list using get request. I edited some details and updated it using put request but now I want that my list should update(make get request) after successful put request.

here is what I am trying:

putConfig(Device) {

    var request =  this._http
        .put(this._puturl+Device.deviceId, Device)
        .map(res => res.json())
        .subscribe(res => this.getConfig() )



    return request;
}

It does't look this work. Can anyone help me out?


Solution

  • What you return is actually a subscription, what you need to do is:

    var request = this._http
      .put(...)
      .switchMap(() => this.getConfig());
    
    return request;
    

    Make sure you subscribe to the result:

    putConfig(Device).subscribe()
    

    To make it 'hot', otherwise it will not trigger.

    Note that I removed the map operation since it is not required, you do not use the res anyways.