Observable Help

#1

I am hoping you can help. I posted a question on Slack but sometimes people are not too helpful.

I want to retrieve the field total from the database. The problem I have having is that the Observable is returning the value after I console it.

How can I get the subscribe line to wait until the data.total has been returned?

checkAvailability(){
   var itemDoc: AngularFirestoreDocument<any>;
   var count: Observable<any>;
   var count2: Number;
  
   itemDoc = this.afs.doc(`classTotal/SessionA-5thGrade`)
   count = itemDoc.valueChanges()
   count.subscribe((data)  => {this.totalRecCount = data.total } );  <-- This Line

   //.subscribe((data)  => {this.totalRecCount = data.total} );
   //.subscribe((res) => { this.totalRecCount = res.total; console.log('Count2: ', res.total)});
   console.log('Count3: ',  count.subscribe(data  => {this.totalRecCount = data.total; console.log("Total Count: ",this.totalRecCount)} ));
  
     return true;
}

0 Likes

#2

In my opinion you don’t need to manually subscribe the Observable because, as written in the docs, ‘Whenever we data-bind to an Observable from UX using the curly brace syntax, we automatically create a subscription to it.
Try to remove the manual subscription to avoid the Observable react before having the result of the dB query and let the Observable react ‘by itself’.

0 Likes