To retrieve a document in Firestore, first you must add the FirebaseTS library to your project. Follow this tutorial to learn how to add FirebaseTS.
To retrieve a document in Firestore, first you must add the FirebaseTS library to your project. Follow this tutorial to learn how to add FirebaseTS.
Once you have added FirebaseTS, you are set to go!
In this section, we will be retrieving documents from Firestore using the getDocument() and the listenToDocument() methods from the FirebaseTSFirestore Class.
getDocument()
Retrieves the document data once, every time it is called.
path - is an string array that points to the location of the document in Firestore.
Every even position of the array represents a collection and every odd position represents a document.
[
1. Import the FirebaseTSFirestore class.
import { FirebaseTSFirestore } from 'firebasets/firebasetsFirestore/firebaseTSFirestore';
2. Declare and initialize a FirebaseTSFirestore object.
3. Call the getDocument() method.
this.firestore.getDocument(
4. Get the data using the onComplete parameter.
this.firestore.getDocument(
Here, we stored the data of the document to a variable call data. Then we got the name and age property from the data object.
Handling code on fail
this.firestore.getDocument(
listenToDocument()
Continuously retrieves the document's data. The method will attached a listener object to the document and retrieve the document data once at the beginning. When there is a change made to the document, it will get called again automatically.
[
onUpdate: result => void - is a callback function that returns an result object that contains the document information. It gets called once at the beginning to get the document and then it will get called again when it detects a change in the document.
How to use
this.firestore.listenToDocument(
How to use