glei
glei

Reputation: 83

Getting error "Cannot read property 'parent' of undefined" when try to remove child

I have function and I want this function remove child when child added to "History"child. I am getting error below,when I add child to "History"child.

Error: enter image description here


Here is my function code :

  exports.onHistoryAdded = functions.database.ref('/Users/Customers/{pushId}/History/{pushID}')
  .onWrite((snapshot, context) => {
  var pushId=snapshot.key;

    return snapshot.ref.parent.child(pushId).remove();
});

enter image description here

Upvotes: 1

Views: 1230

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600100

Your code doesn't seem to have been correctly updated from the beta version of Firebase Functions to the 1.0 release.

In the latest release, the first argument to onWrite is a change and not a snapshot. You can get the latest snapshot from change.after. So:

exports.onHistoryAdded = functions.database.ref('/Users/Customers/{pushId}/History/{pushID}')
  .onWrite((change, context) => {
    var snapshot = change.after;

    var pushId=snapshot.key;

    return snapshot.ref.parent.child(pushId).remove();
});

Upvotes: 4

Related Questions