Crocsx
Crocsx

Reputation: 7609

A specific post call is called twice?

I have an app, and there is many place where we execute post request call.

But, in one place in the application, a call is executed twice :

in my service I have

PostNewStory(params:API_TYPE.post_new_item_history_call){
    let observable$ = this._http.Get(UtilsService.apiServer + '/post_new_item_history', params)
    let subscription = observable$.subscribe((data:any) =>{
        //do stuff
        subscription.unsubscribe();
    });
    return observable$;
}

In My component I have

PostComment(){
    const params = {
       //params
    };

    this._itemDetailsService.PostNewStory(params).subscribe((data:post_new_item_history_response) => {
        this.ResetCommentArea();
    });
}

I have an HttpService like following

public Post<T>(url: string, payload: any): Observable<T>;
public Post<T>(url, payload): Observable<T> {
    return this.http.post<T>(url, payload, httpHeaders)
    .pipe(
        retry(MAX_RETRY)
    )
}

This works perfectly everywhere, I checked, every function is called Once, and only once. If I put a log here =>

public Post<T>(url: string, payload: any): Observable<T>;
public Post<T>(url, payload): Observable<T> {
    console.log("MyLog")
    return this.http.post<T>(url, payload, httpHeaders)
    .pipe(
        retry(MAX_RETRY)
    )
}

I do have only one log. But the call is done twice ?

Upvotes: 0

Views: 845

Answers (1)

Leo
Leo

Reputation: 751

You are subscribing to the http request observable twice, once in PostNewStory, then you return the observable, and subscribe to it again in PostComment.

Try something like this :

PostNewStory(params:API_TYPE.post_new_item_history_call){
  let observable$ = this._http.Get(UtilsService.apiServer + '/post_new_item_history', params)
  return observable$.pipe(
    map((data: any) => {
      // do stuff
      return dataNeededInPostCommentMethod;
    }),
  );
}

Upvotes: 2

Related Questions