Skip to content

Fixed issue calling DataSnapshot methods with null data #1661

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/v1/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,20 +494,31 @@ describe("DataSnapshot", () => {
it("should deal with null-values appropriately", () => {
populate(null);
expect(subject.val()).to.be.null;
expect(subject.child("a").val()).to.be.null;
expect(subject.child("a/b").val()).to.be.null;

populate({ myKey: null });
expect(subject.val()).to.be.null;
expect(subject.child("myKey").val()).to.be.null;
expect(subject.child("myKey/a").val()).to.be.null;
expect(subject.child("myKey/a/b").val()).to.be.null;
expect(subject.child("a").val()).to.be.null;
expect(subject.child("a/b").val()).to.be.null;
});

it("should deal with empty object values appropriately", () => {
populate({});
expect(subject.val()).to.be.null;
expect(subject.child("a").val()).to.be.null;

populate({ myKey: {} });
expect(subject.val()).to.be.null;
expect(subject.child("myKey").val()).to.be.null;

populate({ myKey: { child: null } });
expect(subject.val()).to.be.null;
expect(subject.child("myKey").val()).to.be.null;
expect(subject.child("myKey/child").val()).to.be.null;
});

it("should deal with empty array values appropriately", () => {
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 @@ -128,7 +128,7 @@ export class DataSnapshot implements database.DataSnapshot {
let source = this._data;
if (parts.length) {
for (const part of parts) {
if (source[part] === undefined) {
if (typeof source === "undefined" || source === null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As well as moving this "up" to the parent object I've also changed the check from === undefined to a typeof so it matches the other checks elsewhere:

if (typeof val === "undefined" || val === null) {

if (node === null || typeof node === "undefined") {

return null;
}
source = source[part];
Expand Down