Firebase Functions Tutorial #15 - Upvoting Function (front-end)

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

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 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
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →