Skip to content

change.before.exists() or change.after.exists() is returning false for value 0 #1320

Closed
@deepak786

Description

@deepak786

Suppose you have an onWrite cloud function trigger on a specific path such as /attributes/count.

exports.count = functions.database.ref('/attributes/count')
    .onWrite(async (change, context) => {
        console.log("before data >>>> " + change.before.exists());
        console.log("after data >>>> " + change.after.exists());
        console.log("before data value >>>> " + change.before.val());
        console.log("after data value >>>> " + change.after.val());

        return null;
    });

Set a value of 0 to that path /attributes/count.
See the output of the above function.

before data >>>> false
after data >>>> false
before data value >>>> null
after data value >>>> 0

You can see that the after-data value is 0 but change.after.exists() returns false.

Now try to get the value at path /attributes/count.

const snap = await admin.database().child('attributes').child('count').once('value');
console.log(snap.exists()); // this will give you true for value 0.

So only change.before.exists() or change.after.exists() is returning false for value 0.

As per my testing, working fine on firebase-functions: ^3.21.2 but when I updated to firebase-functions: ^4.1.0, it gave the issue.

Using Node 16

"engines": {
    "node": "16"
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions