Firebase Functions Tutorial #15 - Upvoting Function (front-end)
Key Takeaways
Creates a front-end upvoting function in Firebase Functions
Full Transcript
okay they're my friends so in the last video we created this upvote cloud function right here it's a callable function and we want to now call that from the front end when a user clicks on one of these arrows right here so to do this we're gonna create a function which is registered inside this view instance right here because remember this view instance now controls this section of the UI if we go to our index file over here we can see that this has an ID of app and the view instance over here controls that's element so everything inside it is controlled by the view instance so we're going to create a method inside this view instance which can then be used from this bit of template so you'll see how that works in a second the first thing we need to do is create a methods property which is an object on the view instance and inside this we can register and create different methods of functions that we want to be able to use with inside this bit of template so let's create one and let's call it up volt request because that's what we're voting and we're going to take in an ID because we need to know which tutorial request we want to update and we need to know the ID of that tutorial request so that in the cloud function we can query the database for that record so all I'm going to do for now is log the ID to the console over here so we can see that in a second now we need to invoke this function when we click on one of these arrows on each of these individual list items so remember inside the template we cycle through all of the requests and we output an li tag for each individual request and we have an arrow for each individual request so we can attach a click event to this thing using view by using the view directive so I can say V and then on and it's going to be on a click and we set that equal to something so we're saying here okay well when someone clicks this arrow I won't used to do something and from here I can call this function which is up votes request not voted up volt request and to do that I just need to say up vult request like so and we want to passing the ID of the request well we get access to the individual request so we can say request dot's ID because remember we have the ID property attached to the request because we added it right here so now we're locking that ID to the console so let me see if this all works I'm going to open up the console and make this a bit bigger and refresh over here and now if I click on one of these we see the individual ID of that request document so now we have that and that's the thing we need to send to the cloud function because inside the cloud function we use the ID on the data right here and right here so we need to send that to the cloud function but first of all we need to actually call the cloud function so let's do that we get a reference to it first of all I'm going to call it up fo and set it equal to firebase dot functions invoke that and then we want an HTTP callable and this is called I think just up vote but let's have a look yep up vote so let's copy that and paste it all right here and now have a reference to that cloud function now we want to invoke it but we also want to pass through some data and on that data we want an ID property which is this thing right here now a bit of shorthand because this and this are the same name we can just say ID and JavaScript does the rest for us okay so now we've done that we can catch any errors that happen afterwards so I'm going to say dots catch and then inside the catch block we receive any error now remember inside this cloud function there was a couple of cases that we had an error the first one was if the user was not authenticated and the second one was if the user has already voted that particular request so we can receive that Arab right here and we can do something with the error message for now I tell you what we'll just log the error to the console so console dot log era dots message like so okay so I think this is pretty much done now when we click on the arrow its invoking this function we're getting a reference to the cloud function invoking that passing the ID through as well and catching the error if there's a problem so I'm gonna save this now and the first time I do this then it might take a little while because it's the first time we're invoking the function but thereafter it should start to speed up a little bit and I'm gonna upvote this bottom one node J s so click up votes Oh first of all we need to refresh so we can catch the the changes on the front end okay now let's try it so no js' up volt that it's gonna take a few seconds but we should get a response in a minute and we do it's gone to one cool now if I try to evolve that again then we get an error and it says you can only up vote something once cool so what if I try to vote this one here so that should go to two because I've not previously uploaded that one it was one because we changed it directly in the database before but that's the first time I've voted this one if I try again though then we get an error so I can now only vote things once which is nice that's what I wanted now there's one more thing I want to do and that is to order these in order of votes so the ones with the most votes or votes goes to the top now to do that we have to come down here where we get a reference to the requests collection and we use an extra method on this called order by and then we're going to order by the up votes property that's the property which stores the number of up votes and we're going to order them in descending order that means the highest at the top so de SC for descending so now if I save this and refresh we should see that they're ordered so that the two is at the top then the one then the zero awesome now if I add a new Qwest let's just say this is Python and submit the request we should see that new tutorial request on the page first of all in a second and we do but if I now vote this one laravel six then it should go to one and now laravel six moves above python because obviously the data in the database is changed we get a snapshot a new snapshot of the database and they're ordered by up volts so now everything with the highest volt is going to be above anything with a lower volt so there we go that's the voting system pretty much sorted now I'll tell you what I want to do first of all is sign out and I'm going to register with another person Mario at the net ninja code at UK and test one two three and the reason I'm doing this is to make sure that I can still vote with a different user on these things which I've already voted so can I do that yeah I can do this one I can do this one kinda it's not gonna be a yep it's worked but if I try these again then I still get an error awesome so this all works now now in the next video what I'd like to do is something with this error I'm going to show some kind of little notification at the top a bit like a toast so that if I try to do this numerous times it doesn't just paste an error here it actually shows me on the webpage that I can't do that so we'll do that in the next video
Original Description
🐱👤🐱👤 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
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 AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI