0

I try to using this code but not working

var idUser = event.data.ref.parent.ref.parent.child('idUser').previous.val();

then I get an error when try using

event.data.ref.parent.ref.parent.child('idUser').onDelete(event => {
    event.data.previous.val();
});

This is my full code

exports.makePurchaseSummaryDelete = functions.database.ref('/invoice_data/{pushIdInvoice}/item/{pushIdItem}')
    .onDelete(event => {
    var name = event.data.child('itemName').previous.val();
    var quantity = event.data.child('quantity').previous.val();
    var idUser = event.data.ref.parent.ref.parent.child('idUser').previous.val();
    var refUser = db.ref('/user_data/' + idUser + '/purchaseSummary/' + name.toUpperCase());
    refUser.once("value", function(snapshotUser) {

    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });

    return true;
});

database structure

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hari Nugroho
  • 70
  • 10
  • I can not see the picture – J. Doe Aug 03 '17 at 18:56
  • @Hari: There is no Firebase Cloud Functions tag. Please see the tag [change I made](https://stackoverflow.com/posts/45491967/revisions), since I made it to your previous question too. See my explanation here: https://stackoverflow.com/questions/42854865/what-is-the-difference-between-cloud-function-and-firebase-functions – Frank van Puffelen Aug 03 '17 at 19:19

1 Answers1

1

Database events are automatically passed any data for the node the event triggered (and downwards).

When you go up the parent chain, you will have to explicitly load the data by attaching a listener. That's quite similar to what you already did in your question here: firebase cloud function function looping in execution

That unfortunately means that you cannot get the previous value for nodes higher in the hierarchy than where you triggered the function.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks for your answer, can i get previous data before delete from snapshot? – Hari Nugroho Aug 03 '17 at 19:31
  • Not from a regular `Snapshot`. The previous value is only available in a `DeltaSnapshot`. You can only get a `DeltaSnapshot` from the `event` parameter. Anything from a listener is a regular `Snapshot`. – Frank van Puffelen Aug 03 '17 at 22:50