firestoreGet
The firestoreGet
function is a higher-order function that takes a collection path and an options object, and returns a function that can be used to get a documents from a specified address.
Usage
You can use the firestoreGet
function to get a document from a specified path. The first argument is the slash-separated path of the collection containing the document.
Subcollections
Collections don’t have to be at the top level of the Firestore, adding slashes will set the document in a subcollection. For example, if you want to set a post in a collection of posts for a specific user:
Dynamic Paths
Using subcollections is a common pattern in Firestore since it makes writing security rules much easier. To make our function more reusable, we can use a function that takes an arguments object and returns a string representing the path:
Return Value
The function returns a Promise that resolves with either the expanded document data or undefined
if the document does not exist. The document data will be expanded to include the document ID as a property id
.
Type Safety
If a type parameter is provided, the returned function will be typesafe. Its return type will be a Promise that resolves with the provided type or undefined
.
The provided type definitions will help you to avoid common mistakes, but they are not realtime validation. You can still retrieve invalid data to Firestore if you don’t use the provided functions correctly.