1. 12
    Avoid Duplicating Documents in Firestore
    2m 55s

Avoid Duplicating Documents in Firestore

Jorge Vergara
InstructorJorge Vergara
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

We'll explore what happens when we don't differentiate between a document update and creation leading to duplicated documents in our database.

We'll see how to fix this and make sure that when we send an update to Firestore we don't duplicate data but update the actual document in the database. In our saveTask function we will check to see if the tasks id is new and if it is we'll keep the functionality we have. If the task id isn't new then we have a task already and we'll use updateDoc to update that existing task.

Instructor: [0:00] Now, this can cause some issues for us because if you notice here, we do have the task saved but it actually didn't update the task. It created a new task in the database. If we go back to the previous one, we see that this is Task here, IN PROGRESS description here.

[0:23] This one is Task here, IN PROGRESS description here. It's the same task. If I come here and again, I change...Let's say I'm going to change this to DONE and I click Save Task, I go to the database and here it created a new document. Now, we have three versions of the same document.

[0:47] What we want to do is that we want our saveTask function to do one thing when it is a new task. Like, when it's a new task, it should be doing this exact same thing because we don't have an ID for that task at the moment.

[1:02] If it is an existing task that we are updating, it doesn't need to use the addDoc function. It can just directly update that task in the database.

[1:13] For that, we are going to wrap this into a conditional and we are going to ask if the task ID is new then we are going to do exactly as we were doing. We are going to create a reference to the collection, we are going to use the addDoc function to add this document to the collection, and then we are going to update that document with the new ID.

[1:38] If it is an existing task, we are going to directly create a reference to the task and for that, we use Doc to create the reference. The reference takes the Firestore instance that we are using and it's going to take the path to the task. In our case, this is going to be Tasks/TaskID.

[2:02] Once we have the reference, we can directly call the update function. We pass the reference and here we don't pass value by value because we are going to update multiple things inside of the task. We can just pass this new task object and it's going to update the properties that we are changing.

[2:23] Now, I am going to grab the ID for this one. This is the one that starts with VTX and I'm going to change from IN PROGRESS to TO-DO, from Jorge Vergara to John Doe, and I am going to click Save. Then here I'm going to look for it, VTX and you see that it changed to John Doe, and it changed from IN PROGRESS to TO-DO.

[2:48] Now, it didn't create a new task. It went ahead and updated an existing one.

egghead
egghead
~ 15 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today