Firebase Functions Tutorial #14 - Upvoting Function

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

Creates a new upvoting function in Firebase Functions

Full Transcript

a rather than gang so in this video I want to create another cloud function that is going to help us to add one to view up votes every time a user clicks on an upvote arrow now this is probably going to be the most complex cloud function that we've done in this series so far and that's because there's going to be a fair amount of logic inside the cloud function so first of all we want to make sure that a user is authenticated because only people that are logged in should be able to do this so that's the first step then we only want users to be able to up vote a particular tutorial once so I can't just keep on spam or voting it like that I could only do it once on each tutorial right so every user has one vote for each tutorial that makes sense so we need to first of all check that a user has not voted on a specific tutorial before and we're gonna do that by tracking what tutorials they voted on inside this array right here voted on so for instance if I was to up vote on this then what it would do is it would take the ID of this document from the requests collection if it was this right here it would take this ID and it would add it to that uses up voted on array so if they then tried to vote it again first of all we're gonna check this property right here and make sure that that ID of that request is not inside this array if it's not inside the array we allow them to up vote and we increase the Volt's on that tutorial by one but then what we do is we take the ID and we add it to the up voted on array so then if they try to invoke the same one again it comes over here it checks the up voted on array again and it finds the idea of that request inside here and at that point we're saying no you're not allowed to up vote that particular document or that particular tutorial request again I hope that makes sense so because of all of that there's quite a bit of logic to cover and it is going to be a little bit more complex than the other one so don't feel bad if you have to pause the video just to catch shop and figure out what's going on okay then so now we kind of know what we're doing let's start this so inside index J s our cloud functions file first of all at the bottom I'll do a little comment upvote callable function and then under that we're going to create a callable function and I'm going to call this up volt so exports dot or votes and set it equal to functions so HTTP dots on call and then inside here we pass a callback function that callback function takes a data object and also the context object okay so the first step in here is to make sure that a user is authenticated so let's do a little comment because there's going to be a lot of code inside this function let's make sure it's all organized and we know what each bit is so the first section is going to be to check the auth states now we've done that once before and it's up here so we do a little half check and throw an error if they're not authenticated so I'm going to copy that and I'm gonna paste it down here because we don't need to rewrite things there's no point in that and that's the first step done so the next step is to get references for the user document whoever's tried to vote this and also for the request document the thing they're trying to up vote and remember we need references to both of those because we need to check properties in both of those we need the up voted on property of the user to make sure they've not voted on this particular tutorial before and we also need the actual document that they want to update the up votes on so we need references to both of these collections right now so let's do a comment again and say get refs for user doc and request doc okay so the first one is going to be the user so Const user is going to be equal to admin dot firestore dot collection we want the user's collection and we also want a specific document inside that so doc and we need to pass in the ID of the user to get that document this thing right here now we get that on the context object and to get it we just say context dot auth dot UID so it takes the current user who is logged in and grabs the UID from that user so now we're passing that into the documents method right here to grab a reference to that specific document and we're storing it right here so now we can check data on that document later on okay so the next reference we want is for the actual tutorial request document so const request is equal to admin dot firestore again and then doc collection again this time we want the requests collection and the ID that we want is gonna this time come off the data property so when we make this call later on we're going to attach an ID property to the data that we send to this function and now we can grab it from the data over here so dot doc to grab a specific document data dot ID so it's gonna look for the document with that specific ID and remember if the ID will be passed to this function when we click on one of these arrows if it was this one we'd get the idea of this and send it to the function and we can do that because remember if we go to our public folder JavaScript and then to request CAS we put the ID on to each object inside the request array so we have access to each ID okay so we'll send that along later on alright so that was the next step so what's next well we want to check that the user hasn't already voted on this particular tutorial so we want to check this property over here or voted on and we want to make sure that the ID of the particular tutorial they're trying to up vote isn't inside this array so let us down here say return because remember we always have to return something inside these functions and because we're going to start to do our asynchronous code it's a good idea to start returning things so user dot gets so we have a reference to this document now we're going out and we're getting it and that's asynchronous we attach the then method onto that to fire a callback we takes the documents that it retrieves and we can do something with it and this is where we're going to check that the user hasn't already voted the request okay so let's spell this correctly first of all hasn't and under here the way we're going to check this is by saying if doc which we have access to we get that document and we want the data from the document and we get that by saying data and invoking that method then we can access the voted on property which is this thing over here so that's the array and then we want to check if that includes a specific elements so we can say includes and then we want the data dot ID remember this is the ID of the document the request that we're trying to vote so we're seeing if this array right here includes a string which is this ID now if they've never voted this before it's not going to include that but if they have voted this before it is going to include that and therefore we want to throw an error so I'm going to just grab this right here where we threw an error up at the top and I'm going to paste it over here I'll change this code from unauthenticated to something else and let's delete that so I'll change it to if we scroll through these failed precondition because they have this inside there up voted on array and that's kind of a precondition right and what I'll do is remove this and I'll say you can only vote something once okay so now we're throwing an error if it's already in that array because they can't vote it again so now after that if check if that's not the case it's going to move on right and at this point we want to update the array in the user document so update user array because now we want to add this ID to this array because they've not voted it before so it moves beyond this but now the arab revolt in get so i'm going to add that due to the eraser detracted again this will fail and it was throw the error so let's do that by saying return and it's going to be the user which we grabbed and we want to update that this time so not get but update and we want to update the upvoted on property and now that's going to be an array we want to take the current voted property so we use the spread operator we say dock which we have access to remember we've got the dock right here and then we want to say data to spread out the data properties but we only want the up voted on a rate elements so it's going to grab the current voted on elements and spread those right inside this new array and then we also want and the data dots ID so the ID of the new request that we're adding to this array okay so that's all we need to update and again this is asynchronous so we tack on a dot then method and inside here we fire a callback function which is gonna fire when this is complete and at this point once we've added it to this array then we want to actually update the votes on the request so we want to go to the actual request now and we want to increase that by one so over here we can say update votes on the request like so and then under there we want to return the request itself which is this reference right here we got a reference to that documents and we want to update that again so dots update and the thing we want to update is the up votes that was the property name over here we take a look votes we want to add one to that so how do we increment that by one inside firebase functions well we can do that by saying admin dot fire store dot field value dot increments and then by how many one so what this does is take the current value of updates and increment it by 1 ok so that's pretty much all we need to do so let's just run through this one more time because I know there was quite a lot there we set up the collarbone function and we take in data which has an ID property attached to it that ID represents the ID of the request we want to update we also have the context object with information about authentication first of all we check the authentication state if they're not logged in then we throw an error and we send that back to the user and it doesn't carry on now if they are logged in then we grab references to the user document who is logged in and also the request document that they want to update then we get the user and the document and we check that that upvoted on property for that user doesn't include this ID of the request that they want to update or vote rather now if it already includes that then it throws this error where it says you can only look vote something once if it doesn't include that it's going to move on and at that point then we take that user object and we update it and we update this up voted on property to add this ID of the tutorial request that voting to the array so we're constantly tracking what things they've voted and we're also taking the current items in the up voted on array and spreading those out as well so we don't lose them so once it's done this and it's added that ID then what we can do is we can return this thing right here which is the updates to the request documents and we update the OP Volks property by incrementing it by one so I hope that all makes sense it didn't to me but let's just try it anyway but we can't try it yet because we need to call it because it's an on call function and we'll call it in the next video but what we can do is deploy it so let me come down here and I'm gonna say firebase deploy only functions okay so let's make sure that has been deployed by going to functions and we can see this one right now requests up votes so now we have that deployed in the next video what we'll do is set up the front-end so we can call this function and try everything out

Original Description

Hey gang, in this firebase functions tutorial we'll create a new function - to allows users to upvote a request on the web page. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/firebase-functions/tree/lesson-2 🐱‍💻 🐱‍💻 Other Related Free Courses: + Firebase Firestore - https://www.youtube.com/playlist?list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB + Firebase Authentication - https://www.youtube.com/playlist?list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ + Firebase Hosting - https://www.youtube.com/playlist?list=PL4cUxeGkcC9he0kHAyiyr3dDO2xw0NWoP 🐱‍💻 🐱‍💻 The Net Ninja Community Boards: https://community.thenetninja.co.uk/
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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
How to Open KIT Files (CodeKit Project)
File Extension Geeks
Watch →