0

I have these 2 backend call methods. When I try to debug these, after getting a response from isExisting method

updateDocumentHttp method is calling, but the response is not getting, the control just goes away. Is there any wrong I'm going, or is there any better solution for this?

 this.dataService.processUpdateDocument(isExistingUrl, isExistingCriteria).subscribe();
    
    
    @Injectable()
    export class DataService {

    processUpdateDocument(httpMethod, baseUrl, doc):Observable<any> {
if (httpMethod == 'POST') {
      return this.isExisting(isExistingUrl, isExistingCriteria);
    } else {
      return this.updateDocumentHttp(httpMethod, baseUrl, doc);
  }
}

     isExisting(isExistingUrl, isExistingCriteria): Observable<any>{
          return this.http.post(isExistingUrl, isExistingCriteria)
              .pipe(map((response:[]) =>{
                console.log("Already exist Result: " + response.length);
                if (response.length <= 0) {
                  return this.updateDocumentHttp(httpMethod, baseUrl, doc, options, domainToUpdate, callback, messages)
                 }
              
     }
    
      updateDocumentHttp(httpMethod, baseUrl, doc):Observable<any> {
         return this.http.request<IApiResponse>(httpMethod, baseUrl, {
          body: doc,
          responseType: "json"
         }).pipe(map(
                data => {
                  console.log("Created / Updated : Successfully");
                  if (!domainToUpdate) {
                    app.childScope["domain"] = data;
                  }
                  return true;
                }
            )
          });
      }
    
      }

1 Answer 1

2

Observables won't execute if you don't subscribe to them. In this example, the return type of isExisting method is another observable which needs to be subscribed, so if you use the code below, It will work fine:

this.dataService.isExisting(isExistingUrl, isExistingCriteria)
    .subscribe(result => result.subscribe());
Sign up to request clarification or add additional context in comments.

4 Comments

There is one more update to this question, im not directly calling the isExisting and updateDocumentHttp, There is an another method is calling based on the condtion.
In this situation, It's better to use switchMap instead of map in isExisting method. switchMap operator would subscribe to the new observable and return the result of it, so you don't need to subscribe to it twice.
Thanks, do u have the code for my situation.
No, but if you could provide stackblitz page for it, I would work on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.