2muchcoffeecom / ngx-restangular

Restangular for Angular 2 and higher versions
https://2muchcoffee.com/ngxrestangular-app-development
MIT License
781 stars 110 forks source link

Subscribe result is not working #138

Open xyrintech opened 6 years ago

tairezzzz commented 6 years ago

@xyrintech Could you provide more details? Maybe simulate your issue on stackblitz?

baiolo commented 6 years ago

Hi, I'm also facing troubles with the .subscribe() method, my scenario is:

ngx-restangular is configured as follows:

# Snippet 1

const authRestangular = new Restangular(
          {},
          this.injector,
          this.http
        ).withConfig(restangularConfigurer => {
          restangularConfigurer.setBaseUrl(Costants.BASE_URL);
          restangularConfigurer.setRequestSuffix(Costants.REQUEST_SUFFIX);
          restangularConfigurer.setRequestInterceptor(function (elem, operation) {
            if (operation === 'remove') {
              return null;
            }
            return elem;
          });

          restangularConfigurer.setDefaultHeaders({'Authorization': 'Bearer ' + data.token});
          this.restService.authRestangular = authRestangular;
        });

      },
      error => {
        this.notifier.notify('error', Costants.SHOW_ERROR_LOGIN)
      }

At some point I call:

# Snippet 2

        this.restService.deleteOne({
          service: this.modelService,
          id: this.modelId
        }).subscribe(
          data => {
            this.notifier.notify('success', Costants.SHOW_SUCCESS_DELETE);
            this.modelDeleted.emit(this.modelId);
          },
          error => this.notifier.notify('error', Costants.SHOW_ERROR_DELETE)
        );

The this.restService.deleteOne is doing:

# Snippet 3

  public deleteOne(parameters: { service: any, id: any }) {
    const {service, id} = parameters;
    const resource = this.authRestangular.one(service, id).remove();

    return resource;
  }

With such code I get a weird behaviour: when I call the deleteOne() and the response contains a body (json) and http response code is 200, everything works like a charm. On the other hand if the response has no body and http status code is 204, the content of the .subscribe() method is not executed. I found similar problems regarding Restangular but none of the solutions worked for me.

I've tried various configurations for setRequestInterceptor(), and I've also fully reoved it, but no luck.

NOTE: I'm currently using the setRequestInterceptor() suggested in the docs.