AH.
AH.

Reputation: 803

Remove the current node in cloud function in Firebase onWrite

I want to do a cloud function on the added node on firebase and delete it once the function is over. then((event)=>event.remove()) does not work in the following code.

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.makeUppercase = functions.database.ref('/Q/{pushId}')
  .onWrite(event => {

      const to = event.data.child("to").val();
      const message = event.data.child("m").val();

      const messageTime = Date.now()*-1;
      const messageFromName = event.data.child('fromName').val();

    var updateMessage = {};
    for (var toCounter in to) {
      updateMessage[`/${to[toCounter]}/c/${messageTime}`] = message;
      updateMessage[`/${to[toCounter]}/i/fName`] = messageFromName;

    }

     admin.database().ref().update(updateMessage).then((event)=>event.remove());


  });

Upvotes: 2

Views: 284

Answers (1)

theblindprophet
theblindprophet

Reputation: 7927

You need to call remove() on the reference or parent's reference.

event.data.ref.parent.remove(); or event.data.ref.remove();


So if you have:

"-kwwe323r22g222322": {
    "apples": "apples"
}

Your data would be:

{"apples":"apples"}

and event.data.ref would be:

-kwwe323r22g222322.

Upvotes: 4

Related Questions