Firebase触发删除

时间:2018-11-18 17:25:09

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

我正在尝试创建一个可以识别谁删除的功能,问题是我没有从Firebase服务器得到任何答案,有人可以帮助我吗?谢谢

exports.deleteFunction = functions.database.ref('/clientes')
.onDelete((context) => {
  // Grab the current value of what was written to the Realtime Database.
  console.log("delete");
  console.log(context);
});

这是要删除的功能

confirm = (e) => {
 if (id_deleta) {
      firebaseDatabase.ref('/clientes/categorias/').child(id_deleta)
            .remove();
      notification('success', 'Excluido com sucesso');
      this.callCategoria();
  } else {
    notification('error', 'Ocorreu um erro, tente mais tarde');
  }
}

1 个答案:

答案 0 :(得分:1)

代码更正

onDelete事件处理程序的名称为defined

function(non-null functions.database.DataSnapshot, optional non-null functions.EventContext)

因此,在上面的代码中,.onDelete((context) => {应该为.onDelete((snapshot, context) => {

获取已删除的ID

接下来,如果您试图从id_deleta事件中获取onDelete的值,则可以使用var id_deleta = snapshot.key

exports.deleteFunction = functions.database.ref('/clientes')
.onDelete((snapshot, context) => {
  var id_deleta = snapshot.key;
  console.log("deleted ID %s", id_deleta); // logs "deleted ID 1234", etc.
  console.log(snapshot.val()); // logs the deleted data, no need for this
  console.log(context); // logs the event context
});