LearnTeachCode / pair-partners

A web app that helps people find study partners for virtual pair programming and collaboration
http://learnteachcode.org/pair-partners/
38 stars 16 forks source link

For Rick: Practice displaying Firebase data #31

Closed LearningNerd closed 7 years ago

LearningNerd commented 7 years ago

So you've built your first Firebase app, and with just a few lines of JavaScript code, you already know how to take information from your database and display it on your web page! Woohoo! Now here's some more practice to help you check your understanding of the Firebase functions we used to make it happen.

📚 Prerequisites: First complete "Challenge: Create your first Firebase app!"

✔️ To complete this challenge: follow each step below, check off the checkboxes to track your progress (so we can see your progress too!), and then submit your solution as a pull request.

If you have any questions, post a comment at the bottom of this page or ask us in our Slack chatroom!

1. Set up for local development

Great! Now any commits you make will be saved to this branch and kept separate from other versions of our project.

2. Create another row in your Firebase database

3. Edit your JavaScript and HTML to display the new data

🏆 The goal: Let's change our app to display our two pieces of data in two separate paragraphs that both update in real time when the data changes!

✔️ SOLUTION for Part 3: https://gist.github.com/LearningNerd/d886a3c64e9da6121992f63191e1c70a

4. Create more Firebase reference objects

In Firebase, a reference object represents a specific location in your database. Firebase organizes data in a hierarchy or tree structure, just like your computer's file system or the web browser's Document Object Model (which organizes elements of a web page into a hierarchy, with the document object as the root of the entire tree).

So now that we want to access two sections of our database, we're first going to create a reference to the root of our database, and then we'll use the root reference to create two more reference objects: one for "myname" and one for "greeting".

💡 Remember to commit early and commit often! A good rule of thumb is to make a commit with Git after every 15 minutes of coding and at the end of every work session.

✔️ SOLUTION for Part 4: https://gist.github.com/LearningNerd/f7a3851bf7fbd6c56890546bb21b5719

5. Review event listeners in JavaScript

📚 Reference link: Review these slides on event listeners from our previous JavaScript slideshow!

Remember JavaScript's built-in addEventListener() method? It took two inputs: a string representing the name of the event and a function to run when the event occurs. For example, if you wanted to run the sayHello function each time the user clicks the submitButton on your web page, your code might look like this: submitButton.addEventListener("click", sayHello);

Remember also that your function can receive an input from the event listener, which contains information about the event that just occurred! For example, the event parameter in the code below will be replaced by a MouseEvent object containing lots of information about your mouse clicks:

window.addEventListener("click", logMouseEvent);

function logMouseEvent (event) {
  console.log(event);
}

6. Review anonymous functions

Anonymous functions are just functions defined without a name, which is a common practice in JavaScript when you know you're only going to use the function in one place. Let's compare the previous code sample with a version that uses an anonymous function to do the exact same thing:

Example 1: Named function

window.addEventListener("click", logMouseEvent);

function logMouseEvent (event) {
  console.log(event);
}

Example 2: Anonymous function

window.addEventListener("click", function (event) {
  console.log(event);
});

See how the second version is shorter because of the way we defined that anonymous function? This is a pattern you will see again and again in JavaScript; in fact, the code at the end of your firebasetest.js file is using an anonymous function right now! Let's take a closer look.

7. Create another Firebase event listener

If you know how to use event listeners in JavaScript, you already know how to use event listeners in Firebase! The Firebase event listener is a method named on(), and it's the main way to read data from your database. Right now, the end of your JavaScript file should already contain this code:

dbRef.on("value", function(dataSnapshot) {      
  messageBox.textContent = dataSnapshot.val();
});

Notice that the dataSnapshot parameter works exactly the same as the event parameter in section 5 above; it's given to us by the event listener, and it gets replaced with an object that contains lots of information about the event that just occurred! With Firebase, we don't need to use the entire dataSnapshot object, so instead the convention is to use a special method called val() to extract the data. So when we call dataSnapshot.val(), we're getting the new data from our database and none of the other Firebase information (which we don't need to worry about).

There's just one problem: even though you can see your data in the console, your web page is displaying [object Object] instead of showing your name or your greeting message! Why is that? Because we're using the Firebase event listener on the root of our database, which contains our entire database: {greeting: "Hi from Firebase!", myname: "Liz"}

Notice the format of that data: it's a JavaScript object! We don't want to display the entire database inside one paragraph, though; we want to display a string, one in each paragraph.

✔️ SOLUTION for Part 7: https://gist.github.com/LearningNerd/d088d03deff297e6121068788d136eb6

8. Create a pull request to share your code with us

💡 You can create a pull request at any point! Please share your code with us at the end of every work session, whether or not you've finished the challenge. Pull requests are a great way to ask for feedback on your code!

Once you've submitted your pull request, everyone on our team can share feedback on your code using GitHub's code review and commenting features. So keep an eye out for GitHub notifications in your email inbox!

Congrats, you've completed this challenge!

✔️ Don't forget to check off all the checkboxes in these instructions, so the rest of our team can easily see your progress!

👉 More challenges to come, so stay tuned!

r7uaz0n commented 7 years ago

I think I'm done with this one.