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

Up for grabs: Create an example of the Firebase set() method #50

Open LearningNerd opened 7 years ago

LearningNerd commented 7 years ago

⭐️ This challenge is up for grabs! Claim it by clicking "assign yourself" on the right, under the "Assignees" section for this GitHub issue! You can work on this by yourself or in pairs.

✔️ To complete this challenge: Post a comment at the bottom of this page with some instructions and sample code, so the rest of our team can learn how to use this new Firebase method by reading your comment!

You can edit your comment at any point to update your answer, so you don't have to do these all at once! To edit your comment after you publish it, click the pencil icon in the top right corner of your comment: github-edit-comments

If you have any questions, please post a comment at the bottom of this page! (You can also ask us on Slack, but please post a comment here too so we can more easily reference it later.)

About the Firebase set method

📚 Official documentation: Firebase API section on the set method

The set method saves data to a specified location in the database (using a database Reference object). If that database location doesn't exist yet, the set method will create it! Otherwise, if data already exists there, the set method will replace it with the new data. If that database location contains any children (nested objects), they will all be overwritten with the new data.

Your challenge

🏆 The goal: learn how this method works, test it out for yourself, and then post a comment at the bottom of this page with some instructions and sample code to help teach the rest of our team!

Questions to keep in mind:

kammitama5 commented 7 years ago

Two types of Set :

  1. set()
  2. setWithPriority()

Overwrites data at parent and child location. Does not require inputs -> void function Returns promises

How they are different :

  1. Overwrites data and doesn't have hierarchy, so you have to be careful when using this function
  2. If you'd like to have a starting and stopping point for setting data, to ensure that your data isn't overwritten (or up to a certain point), you should use setWithPriority()
  3. set() generates a value, and a promise upon completion

Example of using Set (Set by individual value)

var dataRef = firebase.database().ref('/users/name');
dataRef.child('first').set('Bob')

(Set simultaneoulsly)

var dataRef = firebase().ref({first : 'Bob', last: 'Smith'});

return promise to see whether the information has been set successfully or not in the database

.then(function() {
 console.log("Success!")
})
.catch(function(e){
 console.log("Failed to set data" + e); // failed and log error
});

return promise Promises can be :

  1. fulfilled (succeeded)
  2. rejected (did not succeed)
  3. pending (not completed)
  4. settled (completed)
LearningNerd commented 7 years ago

Thanks @kammitama5!! The second example code snippet ("set simultaneously") looks like it's missing the call to set() though, and I also notice you set the variable dataRef equal to the result of setting the new data but you probably meant to first create the database reference, and then set the new data. So maybe more like:

var dataRef = firebase().ref("someDatabaseLocationHere");
dataRef.set( {first : 'Bob', last: 'Smith'} );

For the last code snippet, you'll need to add something at the beginning because right now there's no Promise object before the .then -- although I wouldn't worry about using or explaining how to use Promises just yet here!

Another awesome addition to your answers would be to create your own CodePen with a working example to demonstrate using the set() method! :)