How to return value inside subscribe Angular 4

AlejoDev picture AlejoDev · Apr 2, 2018 · Viewed 44.9k times · Source

I'm new to observables in angular. I have a problem wanting to return a value inside a subscribe method. I have the following method (getFirebaseData(idForm:string):observable <any[]>):

getTotalQuestions(idForm:string){
let totalQuestions:number;
this.getFirebaseData(idForm+"/Metadatos")
.subscribe(items => 
  {
    items.map(item => {
      totalQuestions=item.Total;
      console.log(totalQuestions);
    });
  }
);
console.log(totalQuestions);
return totalQuestions;
}

the firts console.log(totalQuestions) print 4 but the second console.log(totalQuestions) print undefined. I understand that subscribe is an asynchronous operation and that for that reason the second console.log(totalQuestions) ("In order to write the code") print undefined, but I can not find the way to return the variable after the subscribe method has been completed. Now, if I change the subscribe to map:

getTotalQuestions(idForm:string){
let totalQuestions:number;
this.getFirebaseData(idForm+"/Metadatos")
.subscribe(items => 
  {
    items.map(item => {
      totalQuestions=item.Total;
      console.log(totalQuestions);
    });
  }
);
console.log(totalQuestions);
return totalQuestions;
}

the firts console.log(totalQuestions) does not print anything and the second console.log(totalQuestions) print undefined. It's something that I do not understand because it happens.

I hope you can help me clarify the concept that I do not understand. Thanks!

Answer

Rajani Kanth picture Rajani Kanth · Apr 2, 2018

You can't directly return totalQuestions like that, you need to use a subject to achieve that.

getTotalQuestions(idForm:string): Observable<string> {
let totalQuestions:number;
var subject = new Subject<string>();
this.getFirebaseData(idForm+"/Metadatos")
.subscribe(items => {
    items.map(item => {

      totalQuestions=item.Total;
      console.log(totalQuestions);
      subject.next(totalQuestions);
    });
  }
);
  return subject.asObservable();
}

Usage: getTotalQuestion(idForm).subscribe((r)=>console.log(r))