Jader Costa
Jader Costa

Reputation: 307

Firebase trigger on delete

Im trying to make a function where I can identify who delete, the problem is Im not getting any answer from the firebase server, someone could help me ? Thanks

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

Here is the function who is deleting

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');
  }
}

Upvotes: 1

Views: 2528

Answers (1)

samthecodingman
samthecodingman

Reputation: 26171

Code correction

The onDelete event handler is defined as

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

So in your code above, .onDelete((context) => { should be .onDelete((snapshot, context) => {.

Getting the deleted ID

Next, if you are trying to get the value of id_deleta from the onDelete event, you can use 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
});

Upvotes: 5

Related Questions