Firebase Firestore Tutorial #5 - Deleting Data
Key Takeaways
Deletes data from Firebase Firestore database
Full Transcript
alright then gang so now we know how to get documents from the fire store and how to add new documents to the fire store via this form now the next thing I want to introduce you to is how to delete documents from the fire store now from a UI perspective this is how I'd like it to work I want to display a little cross in each Li tag over here and when we click on that cross it makes a delete request to the fire store to delete this document from the fire store now remember these Li tags they're being output in this render cafe function right here so for each different document that we receive we're creating an li tag and then to span tags inside the Li tag and we're rendering that to the Dom for each one right here so also for each Li tag I'd like to create another element which is going to have a little X inside it for the cross so let's now create that element I'll say let cross equal to document dot create element and then inside this is going to be a div now it can be a span if you want I've just done it as a div so I can style it differently in the CSS without messing around with and child selectors and things like that so anyway we've created that element now now we need to set the text content of it so I'll say cross doc text content and that's going to be equal to just an X all right finally we need to append this div right here to the li so I'll say Li dot append child and we want to append the cross all right so now if we save that we should see a cross element for every allied tag there it is ok cool now if we click on this at the minute nothing is gonna happen so what we need to do is hook up that functionality for each one of these crosses now where are we going to hook up that functionality well a good idea is to hook it up inside this render Cafe function right here because what we can do is access that individual cross each time around for each document and we can set an event listener to each individual cross so let's do a little comment first of all and this is gonna say deleting data then underneath that we will say cross which is the element we just created and rendered to the dom dot add events listener the event is going to be a click event that we want to listen to and will fire this callback function taking the event object when a user clicks it now the first thing I'm going to do is say stop propagation that just stops the event bubbling up okay next I'd like to say let ID equal to e target dots parent elements like so dots get attribute and I'll explain this in a second and then the attribute we want is the data - ID now what am i doing here well I'm getting an ID variable and I'm setting that equal to the event target which is the cross the element that we click on then I'm getting the parent element of that cross which is the Li then I'm getting an attribute from that Li which is the data ID attribute remember we set that date ID attribute right here so what I'm actually getting is the document ID that is the unique ID for the document inside here this thing so when I click on a cross for example this top one what I'm doing is getting this ID right here so that once I have that ID what I can do is use it to query the firestore and say hey find a document with this ID and then delete it so that's what we want to do now we have the ID now we just need to query the firestore and delete that document so DB dot collection because we want to go into the caffee's collection again then what we want to do is find an individual document now how do we do that because previously we've just been retrieving or using the full collection now this time we want to find one document inside that collection now to do that we say dot doc this is how we find a single document and we pass the ID of that document inside here so we can pass through the ID like so cool so now we have a reference to the document with that ID so whichever document the cross corresponds to so now we have that reference we can just use a method called delete and that will go ahead and that document alright so let's try this out in the browser I'm just gonna try and delete Shawn's magical coffee Emporium I'll press the cross obviously nothing happens on the front end because we're not hooking up to real-time updates in the database yet but if we go to the database we can see one less record now here there's three over here and we can't find Shawn's magic coffee Emporium anywhere now if we refresh over here it's gone cool so again what we're doing is we're creating now across for every single Li tag that we render every single document that we retrieve so they're all getting their own cross and we're appending that cross to that Li now we're setting up an event list it's a each individual cross and that event listener is listening for a click event we're stopping the propagation of that event so it doesn't bubble up and then we're using this ID right here to get that ID from the Li tag and then we're using the ID to grab this document and then delete it does that make sense good alright then so now we know how to retrieve data save data and delete data in the next video I want to show you how to perform slightly more complex queries
Original Description
Hey all, in this Firebase Firestore tutorial I'll show you how to delete data from the database.
🐱💻 Course Links:
- VS Code editor - https://code.visualstudio.com/
- GitHub repository (course files) - https://github.com/iamshaunjp/firebase-firestore-playlist
- JavaScript DOM tutorial - https://www.youtube.com/watch?v=FIORjGvT0kk&list=PL4cUxeGkcC9gfoKa5la9dsdCNpuey2s-V
🤑 Donate @ https://www.paypal.me/thenetninja
🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
The Case of the Ghost Tooltip
Medium · JavaScript
How I made a scroll-scrubbed video portfolio fast (Next.js 15 + GSAP + canvas)
Dev.to · Pratham Sharma
5 Reasons HTML Is About to Change Frontend Development
Medium · Programming
5 Reasons HTML Is About to Change Frontend Development
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI