Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
deepak786 opened this issue Dec 1, 2022 · 1 comment

Comments

@deepak786
Copy link

deepak786 commented Dec 1, 2022

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"
  }
@exaby73
Copy link
Contributor

exaby73 commented Nov 9, 2023

Duplicate of #1407. This issue is fixed. Thank you

@exaby73 exaby73 closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants