Noman Ali
Noman Ali

Reputation: 3340

Which value changed in Child_changed firebase

My function is listening if any value of users is changing. But I want it to be more specific to trigger only if specific value is changed.

For example, I want it to trigger if only phone is changed (do not trigger if anything except phone is changed).

Current code is below:

var usersref = firebase.database().ref('users');
usersref.on("child_changed", function(snapshot) {
   var userid = snapshot.key;
   var user = snapshot.val();
   console.log("The updated user name is " + user.name);
});

If phone of user is changed, it should trigger. How do I do it?

Upvotes: 1

Views: 867

Answers (1)

Callam
Callam

Reputation: 11539

You could create another root node called /user-phones, and update the corresponding user's phone number there and in their profile, or you could use Cloud Functions to fan-out the changes made to /users/$uid/phone to /user-phones/$uid/phone automatically.

Now instead of listening for child_changed on /users, you listen to /user-phones which will only trigger when a user changes their phone number.

Upvotes: 2

Related Questions