删除Firebase onWrite中云功能中的当前节点

时间:2017-11-02 12:22:50

标签: javascript firebase firebase-realtime-database google-cloud-functions

我想在firebase上添加的节点上执行云功能,并在功能结束后将其删除。 then((event)=>event.remove())在以下代码中不起作用。

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());


  });

1 个答案:

答案 0 :(得分:4)

您需要在引用或家长的引用上调用remove()

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

所以如果你有:

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

您的数据将是:

{"apples":"apples"}

event.data.ref将是:

-kwwe323r22g222322