Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Update or Create

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

like image 325
Lee Avatar asked Feb 24 '17 17:02

Lee


People also ask

What is the difference between set and update in Firebase?

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?

What is the difference between set and update?

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.

Which method is used to update Firebase data?

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.


1 Answers

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

like image 185
Sebastian Gomez Avatar answered Oct 16 '22 11:10

Sebastian Gomez