Skip to content

Commit cae4158

Browse files
authored
RTDB exists() should return true for falsy values (e.g. 0, false, "") (#1410)
Fix for #1407
1 parent 9e2f797 commit cae4158

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Diff for: spec/v1/providers/database.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,15 @@ describe("DataSnapshot", () => {
580580
populate({ a: [{}] });
581581
expect(subject.child("a").exists()).to.be.false;
582582
});
583+
584+
it("should be true for a falsy value (other than null)", () => {
585+
populate({ num: 0, bool: false, n: null });
586+
expect(subject.exists()).to.be.true;
587+
expect(subject.child("num").exists()).to.be.true;
588+
expect(subject.child("bool").exists()).to.be.true;
589+
expect(subject.child("n").exists()).to.be.false;
590+
expect(subject.child("missing").exists()).to.be.false;
591+
});
583592
});
584593

585594
describe("#forEach(action: (a: DataSnapshot) => boolean): boolean", () => {

Diff for: src/common/providers/database.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class DataSnapshot implements database.DataSnapshot {
159159
*/
160160
exists(): boolean {
161161
const val = this.val();
162-
if (!val || val === null) {
162+
if (typeof val === "undefined" || val === null) {
163163
return false;
164164
}
165165
if (typeof val === "object" && Object.keys(val).length === 0) {

0 commit comments

Comments
 (0)