React devs might find this archived course helpful to reference, but this course has been updated! We now recommend you take the Firebase Fundamentals course.
Transcript from the "Update a Document Solution" Lesson
[00:00:00]
>> Steve Kinney: All right, so our mission is to be able to repeatedly star things. Let's flip back over and take a look. So if I go into this individual post, I had this removed. We can call this one, let's just call it star. So we have a number of stars that we pass into the component from the application state.
[00:00:29] So what we'll do in this case, we'll try postRef.update and that will take an object of the fields that I want to update. So in this case, we can do stars:
>> Steve Kinney: stars + 1. All right, let's take that for a spin. We actually have to put it on the button before it's gonna do anything.
[00:00:56] So scroll down.
>> Steve Kinney: We'll say onClick,
>> Steve Kinney: Star. The nice part is because the database is gonna automatically push out the updates, I don't have to do anything else. I just save the change I want to happen. It will change Firestore. Fire store will tell the application component things have changed.
[00:01:19] That'll update the application state. And we should, famous last words, be good to go. Here's the thing, is that if you just, one if you share your key, other people can push stuff, and two, is if you don't actually,
>> Steve Kinney: Wrap, it's wrapped in a function, let's see.
[00:01:44] It's just I have friends that are using my API key. So if I click it once it goes up once, [LAUGH] but when you push up your code and let other people pull it down [LAUGH], you could have friends that are also modifying your database. Let's hope everyone's playing.
[00:02:01] Cool. Few things to fix is one, you can see that invalid date there. There's a really good reason for that, which is when it was in application state, we were just using JavaScript Date Objects. And if we go over here and we can see that we're just using moment to format the date, but we're not using JavaScript Date Objects.
[00:02:23] If I need to take a Firebase object and the Firebase timestamp and use it to a real timestamp, I just have to call the toDate method on it, right? And that will put it back to a JavaScript date. These are those more accurate ones. Remember when I had to do the timestamps and snapshots, to use the Firebase ones?
[00:02:43] Now I have to turn it back into a JavaScript date. All right, so we have the ability to create, to read, to update, and delete. We have blown through the quota, which is fun. So I can figure out, we'll figure out the fun parts in that in a little bit.
[00:03:02] But let's talk a little bit about authentication.