Skip to content

Commit

Permalink
RTDB exists() should return true for falsy values (e.g. 0, false, "") (
Browse files Browse the repository at this point in the history
…#1410)

Fix for #1407
  • Loading branch information
inlined committed Jun 12, 2023
1 parent 9e2f797 commit cae4158
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spec/v1/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,15 @@ describe("DataSnapshot", () => {
populate({ a: [{}] });
expect(subject.child("a").exists()).to.be.false;
});

it("should be true for a falsy value (other than null)", () => {
populate({ num: 0, bool: false, n: null });
expect(subject.exists()).to.be.true;
expect(subject.child("num").exists()).to.be.true;
expect(subject.child("bool").exists()).to.be.true;
expect(subject.child("n").exists()).to.be.false;
expect(subject.child("missing").exists()).to.be.false;
});
});

describe("#forEach(action: (a: DataSnapshot) => boolean): boolean", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/common/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class DataSnapshot implements database.DataSnapshot {
*/
exists(): boolean {
const val = this.val();
if (!val || val === null) {
if (typeof val === "undefined" || val === null) {
return false;
}
if (typeof val === "object" && Object.keys(val).length === 0) {
Expand Down

0 comments on commit cae4158

Please sign in to comment.