Firebase with React, v2 QuerySnapshots
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 "QuerySnapshots" Lesson
>> Steve Kinney: So QuerySnapshots are an object. There's basically two major objects that we're gonna get back from Cloud Firestore, right? Well we just said .collectionpost, we got a reference to that place in the database but we didn't get anything from the database. Those previous ones we saw before was post and then I like an idea of a given document and that was a reference to that place in the database, but it wasn't necessarily the data.
[00:00:27] When we request the data with a git, or try to put data with an update or a set, or even delete it with a delete, right, we'll delete, you're not gonna get back data, but if we request it, we’re going to get back a snapshot. Either if it’s for a collection of things, we’ll get back a QuerySnapshot and if we request one document, we will get back a document snapshot, which is just an object that represents our data.
[00:00:51] And QuerySnapshots, they have a few properties and a few methods. And I just wanna talk about them a little bit before we start playing with them so we kinda have a sense, basically we can see the forest for the trees before we get too deep into the code.
[00:01:06] QuerySnapshots have a property called docs, that is an array of document snapshots, all right, that makes sense. And we have a property called empty, that is true if the query was empty, and it is false if it's not. Seems so far so good. Metadata is just where it came from, so on and so forth.
[00:01:29] Query is basically if you needed to get back to the actual query that you fired to go ahead and refine it and do something else on those lines as a reference back to that query object. And the size is the number of documents in the QuerySnapshot. So those are properties, there's also some methods.
[00:01:44] The methods are docChanges, this will actually give you back, it'll fire an event, basically, again, add it and give you a document, removed, updated, right? For each, we'll go ahead and let you iterate through, then isEqual will let you take another query snapshot and see if they're equal.
[00:02:24] We're gonna be using docs and just kinda replacing them, but you can see a world where if using something like redox, right, and you're firing events of like document added, stuff along those lines, you could theoretically use those and fire the actions, right? And if you're implementing something like undo or something along those lines, having the individual things that changed might be more useful to you than just the latest state of the world.
[00:02:49] For our purposes we're going to take the latest state of the world today, like here are all the new documents, update the state of our application. And that is generally speaking for a lot of applications what you don't want, but you can imagine something, again, where if you wanted to have previous revision history client side you can keep all those so on and so fourth, right?
[00:03:08] Those are all options as well.