Home > other >  Angular http method calling inside method not getting any response
Angular http method calling inside method not getting any response

Time:10-09

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;
                }
            )
          });
      }
    
      }

CodePudding user response:

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());
  • Related