How to search for specific value in DataSnapshot with Firebase cloud features

I am trying to create a cloud function that will be triggered on an HTTP request (which is sent on a timer) that will remove all children with a specific value.

The node database looks like this:

activities
    4GI1QXUJG0MeQ8Bq19WOdCQFo9r1 //uid
        activity: "hammer"
        id: some ID
        note: "some note"
        timestamp: some timeintervalsince1970
    7IDUiufhiws8939hdfIUHiuhwdi5
        etc....

      

I want to view all activities, and if the activity value is "hammer", I want to delete it.

This is what I still have

exports.deleteHammerNotifications = functions.https.onRequest((req, res) => {

    admin.database().ref('activities').once('value', (snapshot) => {

        console.log(snapshot.val())

    });
});

      

which prints:

{ 
    '4GI1QXUJG0MeQ8Bq19WOdCQFo9r1': 
      { activity: 'nn',
        id: '4GI1QXUJG0MeQ8Bq19WOdCQFo9r1',
        note: 'Blank note...',
        timestamp: 1498032472 },
     M6xQU5XWTEVbSqBnR3HBAEhA9hI3: 
      { activity: 'hammer',
      id: 'M6xQU5XWTEVbSqBnR3HBAEhA9hI3',
      note: 'some note here...',
      timestamp: 1497973839 },
}

      

My problem is that I don't know how to loop through the DataSnapshot and search for all children that have an activity: hammer value. I made a similar function in my xcode project with arrays, but I don't know how to do it using JavaScript.

Any help is appreciated!

+3


source to share


2 answers


To loop through the appropriate child nodes use snapshot.forEach()

:

exports.deleteHammerNotifications = functions.https.onRequest((req, res) => {
    admin.database().ref('activities').once('value', (snapshot) => {
      snapshot.forEach((childSnapshot) => {
        console.log(childSnapshot.val())
      });
    });
});

      

But you still haven't specified a prompt here to select the correct nodes. Without such a request, you can also call admin.database().ref('activities').remove()

.

To most efficiently remove multiple items from the database and write one response back to the user, use this function (which I changed from something I needed lately):



exports.cleanup = functions.https.onRequest((req, res) => {
  var query = admin.database().ref("activities").orderByChild("activity").equalTo("hammer");
  query.once("value").then((snapshot) => {
    console.log("cleanup: "+snapshot.numChildren()+" activities");
    var updates = {};
    snapshot.forEach((child) => {
      updates["activities/"+child.key] = null;
    });
    admin.database().ref().update(updates).then(() => {
      res.status(200).send(snapshot.numChildren()+" activities deleted");
    }).catch((error) => {
      res.status(500).send(error);
    })
  });
});

      

More details:

+3


source


I'm not sure if this is possible, but if you can trigger the "child_added" listener, after the HTTPS trigger has run, you can do it.

ref.on('child_added', function(snapshot) {
 if (snapshot.child("activity").val() === 'hammer') {
 var value = snapshot.child("activity").val();
})

      



I do the same to find out if all people are subscribing to my mailing list or not, and if they are, they will receive mail.

Hope it helps :-)

-1


source







All Articles