Open LearningNerd opened 7 years ago
Two types of Set :
Overwrites data at parent and child location. Does not require inputs -> void function Returns promises
How they are different :
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 :
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! :)
⭐️ 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:
❓ 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
methodThe
set
method saves data to a specified location in the database (using a database Reference object). If that database location doesn't exist yet, theset
method will create it! Otherwise, if data already exists there, theset
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: