1

I have two components that share a service. One of the components gets an entire collection from Firestore as an observable.

The second component should display all the data for a single document. However, I am not sure how I can achieve this without querying the database again.

I have tried using pipe(map(documents => documents.find(...matching logic...))) on the existing observable, but it still queries the database again.

So TL;DR: I query an entire collection, how can I get a single document from that without querying the database again?

4
  • Cache the "entire collection" response in the service (possibly using an observable). Use the cached data in the second component. Commented Jun 6, 2020 at 23:02
  • 1
    Pipe shareReplay(1) Commented Jun 6, 2020 at 23:05
  • That did the trick! Thanks! Commented Jun 6, 2020 at 23:13
  • 1
    @NielsKersic please add the solution as an answer so others can see that the question is solved, stackoverflow.com/help/self-answer Commented Jun 8, 2020 at 8:41

1 Answer 1

3

By piping in shareReplay(1) when first retrieving the collection, subsequent requests will use a locally cached collection instead of requesting the data from the database again.

The first request will look something like this

this.angularFirestore.collection('collection')
  .valueChanges()
  .pipe( shareReplay(1) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.