Ajoe
Ajoe

Reputation: 1545

calling a method inside a subscribe function is not waiting to get the result in angular 4

I am very new in angular 4. I am fetching a result set using a subscribe method and inside that I need to call another method to manipulate the first result set. But before getting the second result set, the process is getting completed.

My code in component.ts is

workItems: IGeneratedTasks[];
this._taskManagementService.getAllWorkItems()
  .subscribe(workItems => {
    this.workItems = [];
    if (workItems != null)
      workItems.forEach(result => {            

        this._taskManagementService.hasAttachment(result.id).subscribe(count => {
          console.log(count);
          if (count > 0)
            result.hasAttachment = true;  //hasAttachment is a property in IGeneratedTasks
        });         


        console.log(result.hasAttachment);


      });
    this.workItems = workItems;

  },
  error => this.errorMessage);

my service.ts is

getAllWorkItems(): Observable<IGeneratedTasks[]> {       
    return this._http.get(this._workItemUrl)
        .map(res => res as IGeneratedTasks[] || [])
        .map(tasks => tasks.map(this.generatedTasksService.toGeneratedTasks))
        .catch(this.handleError);
}

 hasAttachment(id:Number): Observable<number> {       
    let myHeaders = new HttpHeaders();
    myHeaders.append('Content-Type', 'application/json');
    let myParams = new HttpParams();
    myParams = myParams.append('workItemId',id.toString());

    return this._http.get(this._hasAttachmentUrl,{ headers: myHeaders, params: myParams })
        .map(res => res)           
        .catch(this.handleError);
}

Am I calling the methods in the right way?

Small change after the suggestions,

this._taskManagementService.getAllWorkItems()

  .subscribe(workItems => {
    this.workItems = [];
    let newWI: IGeneratedTasks[] = [];
    if (workItems != null) {
      workItems.forEach(result => {
        result.isOverDue = result.duedate != null && result.duedate < (new Date()) && result.overallstatus != "4";
        workItems.forEach(result => {
          this._taskManagementService.hasAttachment(result.id).subscribe(count => {
            if (count > 0) {
              result.hasAttachment = true;
            }
            newWI.push(result);
          });

        })
      });

    }
 this.workItems = newWI;

    },
  error => this.errorMessage);

I have a similar call like hasAttachment

  this._taskManagementService.hasNote(result.id).subscribe(count => {
            if (count > 0) {
              result.hasNote = true;
            }                
          });

Where do I add this, so that the final dataset contains both the hasAttachment and hasNote.

Upvotes: 0

Views: 2143

Answers (1)

Ajoe
Ajoe

Reputation: 1545

I resolved my issue, the code is

this._taskManagementService.getAllWorkItems()

  .subscribe(workItems => {
    this.workItems = [];
    let newWI: IGeneratedTasks[] = [];
    if (workItems != null) {
      workItems.forEach(result => {
        result.isOverDue = result.duedate != null && result.duedate < (new Date()) && result.overallstatus != "4";
        workItems.forEach(result => {
          this._taskManagementService.hasAttachment(result.id).subscribe(count => {
            if (count > 0) {
              result.hasAttachment = true;
            }
          });
          this._taskManagementService.hasNote(result.id).subscribe(count => {
            if (count > 0) {
              result.hasNote = true;
            }                
            newWI.push(result);
          });
        })
      });
    }
    this.workItems = newWI;

  },
  error => this.errorMessage);

Upvotes: 1

Related Questions