Write, Update, and Delete data from Cloud Firestore

Learn to write, update, and delete data to the Cloud Firestore database and discover the different ways to perform write operations.

To carry out any operation on the Cloud Firestore database, we must create a reference to the specific path on our database. We covered how to create Firestore references in detail in the previous lesson. Feel free to head back for a refresher!

Write to the database

There are multiple ways to write to the Cloud Firestore. The method used depends on the behavior we want for our application. Let’s discuss these methods.

Use the setDoc function

The setDoc function writes to the document specified by the path to which its reference points. This function takes a reference to the database document as its first parameter. Its second parameter is an object with key-value pairs. These pairs represent the data we want to write.

The setDoc function is imported from the firebase/firestore subpackage and returns a JavaScript promise that resolves when the data has been written successfully to the database. We can handle this using JavaScript’s async-awai syntax or a .then callback.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy