Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Realtime Database - Find and Update

I am testing out the firebase's real-time database and I am having a real hard time. First I would like to know what is the best approach to finding a record and update it with other info. Let's say I have a user and with a post request, I want to change its name.

I tried:

var userRef = admin.database().ref('users');
var email = req.body.email;
var name = req.body.name;
userRef.equalTo(email).update({name: name});

Try to follow the official documentation but with no luck.

My database:

users
   -Lbq98URniAej2TkWBhG
      email: "[email protected]"
      name: "Pepe"
-Lbq9GC1A131De-iumI0
      email: "[email protected]"
      name: "Pipa"

Also, mind that I don't know the uid beforehand, so I have to look into the child data.

like image 904
Ezequiel Ramiro Avatar asked May 08 '26 16:05

Ezequiel Ramiro


1 Answers

First I would like to know what is the best approach to finding a record and update it with other info.

I am not sure whether this is the best approach or not but for your situation I would have set up my database in this way

users
     -- unique user id - 1      
        -- name
        -- email
        -- posts
           -- unique post id
     -- unique user id - 2      
        -- name
        -- email
        -- posts
           -- unique post id
// and so on

Then I would use the user id to change name like this.

var database = admin.database();
var name = req.body.name;
var uid = req.body.uid;
var userRef = database.ref('users/' + uid);

userRef.update({name : name})
    .then(() => {
        console.log("Update Successfull")
    })
    .catch(error => {
        console.log(error);
    });

I am guessing your database organized in such way:

users
     -- pxTYaszxcsauniqueId1
        -- email    
        -- name
        -- posts
           -- unique post id
     -- abcdeFFFaKauniqueId2     
        -- email
        -- name
        -- posts
           -- unique post id
// and so on

In that case I would use the email to find the document, doing something like this:

// Get a reference to the database service
var database = admin.database();
var name = req.body.name;
var email = req.body.email;

var ref = database.ref("users").orderByChild("email").equalTo(email);

ref.once("value").then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {

      childSnapshot.ref.update({
        "name" : name
      }).then(function() {
        console.log("Success")
      });

      // Cancel enumeration
      return true;
  });
})

Again this is assuming the way you have organized the database. If you are organizing the db in a different way the above code will not work. If its different then please do add the way you have organized the db in the question that you have posted.

like image 122
Shababb Karim Avatar answered May 11 '26 06:05

Shababb Karim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!