i am using Firebase and Node.
I would like to use the same method to either update an object, or create it, if for some reason it does not exist.
Consider the following method
const firebaseSave = async function(data) {
const uid = firebase.auth().currentUser.uid
const profilePath = `users/${uid}`
const profileData = {
name: data.name,
}
const userRef = firebaseDb.child(profilePath)
await userRef.set(profileData)
}
What would be the best and correct way to determine if update or set should be called?
thanks
In RESTful terms “set” is like an PUT, and an “update” is like a PATCH, but there is a bit more interesting distinctions under the covers. Firebase… pirate…. PATCH… get it?
set is a method that takes one argument which is the value to be set. The store value gets set to the value of the argument if the store value is not already equal to it. update is a method that takes one argument which is a callback.
Update specific fields. To simultaneously write to specific children of a node without overwriting other child nodes, use the update() method. When calling update() , you can update lower-level child values by specifying a path for the key.
basically with:
"set" you write or replace data to a defined path, like messages/users/
, you can update the information or create it.
Check this out: https://firebase.google.com/docs/database/admin/save-data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With