Skip to main content

Usage

You can use this to create a function that adds a post to a collection of posts. The first argument is the slash-separated path of the collection.

Subcollections

Collections don’t have to be at the top level of the Firestore, adding slashes will add the document to a subcollection. For example, if you want to add a post to 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:
Even more convenient is to pull the userId from the post itself, assuming the post contains it. This can be done by using the data property of the arguments object which contains the data that will be written to the document:

Return Value

The function returns a Promise that resolves with a DocumentReference to the newly created document.

Type Safety

If a type parameter is provided, the returned function will be typesafe:
The provided type definitions will help you to avoid common mistakes, but they are not realtime validation. You can still write invalid data to Firestore if you don’t use the provided functions correctly.

Options

The second argument to firestoreAdd is an optional object allowing for additional configuration. This object can have a property addMetadata which, if true, adds metadata to the document.
If the addMetadata option is not specified, the function will operate without adding any additional metadata to the document. This default behavior is suitable for scenarios where metadata is not required, offering flexibility based on your needs.

⚠️ Troubleshooting

Here are some common issues you may encounter when using firestoreAdd:
  • Invalid Paths: Ensure your collection and document paths are correctly formatted and exist in Firestore.
  • Type Errors: If you encounter type errors, verify that the data passed matches the defined type schema.
  • Promise Rejection: Network issues, permission errors, or invalid data can cause promise rejections. Ensure you have appropriate error handling in place.
  • You can see the implementation of firestoreAdd here.
  • Review more information on adding data to Firestore here.