Getting Started with Firebase 9 #9 - Fetching a Single Document

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right then so we've seen now how to go out and grab an entire collection of documents using this function on snapshot and also previously using the get docs function as well so that's how we get a full collection of documents but what if we just want to go out and grab one document well we can do that all we have to do is import another function up here from firestore this time it's called get doc and this function grabs us a single document so let me come down here and we'll flesh out this code now the first thing we need to do is make a document reference much like we did when we deleted a document we created a document reference where we passed in the database the collection and the id of the document into this doc function right here so we need to do the same thing down here for the document that we want to get so i'm going to create that i'll say const doc ref and this is not going to interfere with this constant right here because this is a local variable inside this function so doc ref is equal to doc then we invoke that we pass in the db then the collection which is books and then we want to pass in a document id now i'm just going to pass in an id i've already grabbed from one of our documents so it's going to go out and get a reference to that particular document and then what i want to do is come down here and use the get doc function to go out and get that document and all i need to do is pass in the doc ref like so now this returns a promise so we can tack on a then method to fire a function when we have that document and this function takes in the document as an argument so we can do something with it all i'm going to do is log it to the console so we'll log out the doc.data remember that's a function to get that data and then also the doc id all right then so let's see if this works so now in the console we still see the entire collection because we still have that subscription set up to the entire collection but underneath that we also have the single document that we just grabbed and notice this is the id i used to get the doc reference so that's how we can grab a single document now much like before when we set up a subscription to a collection to get real-time data we can also do that to a document as well i could subscribe to a single document so that if that document ever changes then firestore will send us back a new version of that document after it's been changed for example if someone changed the author property then it would send us back the document again so we could reuse that updated data now the way we do this is very very similar to how we created a subscription to a collection we use this same function right here on snapshot so i'm going to copy that and come down here and i'm going to paste it in and then we need to invoke that function as well now inside here all we have to do is pass in the doc ref instead of the collection ref when we pass in a collection ref that sets up a subscription to a collection but now we're just subscribing to changes on a particular document so as a second argument all we have to do is pass in a function which is gonna fire every time firestore sends us back a new version of the document so every time that document changes in the database and all i'm going to do is log that to the console when it happens so console.log doc dot data which is a function and then also doc.id much like we did up here and in fact i'm going to get rid of that now because we don't need that anymore because we're setting up a real-time listener to this document instead and remember this also fires once initially when we first load the page so we get that document once to begin with and then thereafter every time there's a change to that individual documents in the firestore collection okay so i've actually got open two windows here i've got the firestore over here on the right and i've got open this document which we have a real-time listener set up to and over here we see in the console we have that document and that's the document that we're subscribing to right so we get it once initially which is over here in the console so now if i try to change this let me just come to this and i'll change this to a capital t and then i'm going to update it now because we're changing this then firestore should send us back a new version of the document and we should fire that function inside the on snapshot function so let me update it and we can see now we get back a new version of the document and we can see the title is updated notice also we got back this set of documents as well and that's because we have a listener set up to the whole collection and the collection just changed as well so if i had that function to relog out all of the new data inside that collection and also the function right here to log out the single document as well
Info
Channel: The Net Ninja
Views: 3,565
Rating: undefined out of 5
Keywords: firebase, firebase tutorial, firestore tutorial, firestore, tutorial, firebase 9, firestore 9, firebase 9 tutorial, firebase auth, firebase auth tutorial, firebase auth 9, firebase version 9, auth tutorial, firebase 8 vs 9
Id: lZzd8zurolg
Channel Id: undefined
Length: 4min 49sec (289 seconds)
Published: Wed Nov 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.