kurrodu kurrodu - 2 months ago 17
Javascript Question

Detect value of child changed from false to true

How can I detect if the value of the child i.e.

userStatus
is changed from
false
to
true
in firebase database.

Below is the code that I trying to use, but it doesn't work. As, I understand, when the
userStatus
is changed from
false
to
true
, the below expression is no longer true and hence doesn't get triggered.

Can someone please suggest, how I could achieve this ?

usersRef.orderByChild("userStatus").equalTo(false).on("child_changed", function(snapshot) {
var userDetails = snapshot.val();
var firstname = userDetails.firstName;
console.log("userDetails, firstname: "+firstname);
});


[EDIT]

Below is my firebase db structure, where I am trying to trigger a
child_changed
event only when the
userStatus
changes from
false
to
true
.

<code>Firebase db structure</code>

Answer

One solution would be to use a query that matches users with a userStatus of false and to then listen to the child_removed event. You will be notified when a user changes and no longer matches that query. That is, you will be notified when the userStatus changes from false to true.

usersRef.orderByChild("userStatus")
  .equalTo(false)
  .on("child_removed", function (snapshot) {
    var userDetails = snapshot.val();
    var firstname   = userDetails.firstName;
    console.log("userDetails (status changed to true), firstname: " + firstname);
  });