Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LearningNerd opened this issue Jun 15, 2017 · 2 comments
Open

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

LearningNerd opened this issue Jun 15, 2017 · 2 comments

Comments

@LearningNerd
Copy link
Member

LearningNerd commented Jun 15, 2017

⭐️ 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:

  • What does the official documentation say about this method?
  • When would we need to use this method? What are some example use cases? (Write some sample code for us, please!)
  • When should we not use this method? Can you think of any scenarios where using this method would cause errors or unintended side effects?
  • How many inputs does this method require, if any? What data types can the inputs be?
  • What does this method return as an output, if any? What data type is returned?
  • If there are different options for inputs, how does this method behave differently depending on the different possible input(s)?
@LearningNerd LearningNerd changed the title Up for grabs: Create an example of the Firebase set method to insert or update data Up for grabs: Create an example of the Firebase set() method Jun 15, 2017
@kammitama5 kammitama5 self-assigned this Jun 17, 2017
@kammitama5
Copy link
Collaborator

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
Copy link
Member Author

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants