Search code examples
angulartypescriptgoogle-cloud-firestoreobservableangularfire

How to get a variable from angularfire, even though it is an observable?


So, I don't know anything about angular much less how to select a specific field from my DB as it comes in Observable. Tried everywhere to see an example but to no avail.

I'm using angular + firebase, with the angularFire library. There are examples in the documentation that I couldn't adapt.

This is my code made by following it.

constructor( private aff: AngularFirestore, public crud: CrudService ) { 
this.prodCollection = aff.collection<Product>('prods');
this.prods = this.prodCollection.snapshotChanges().pipe(
  map(actions => actions.map(a => {
    const data = a.payload.doc.data() as Product;
    return { ...data };
  }))
);
console.log(this.prods);
}

[This is a message you receive on the console. Click Here

I need to get only one "variable", how can I do that? With data=> res => or any recommended


Solution

  • So... I managed to solve my problem by adapting the answers that were sent to me. At first I had copied the direct example from the firabase documentation but I still couldn't transform the result into an array.

    this is how it appears:

    private itemsCollection: AngularFirestoreCollection<Item>;
    
    
    items: Observable<Item[]>;
    

    to work properly I changed the array from Observable<Product[]> to Product[]

    private prodCollection: AngularFirestoreCollection<Product>;
    
    array!: Product[];
    

    Then I used the first example sent by JSmith to complete the problem

    I don't know if it's the correct way but it's my solution.