Skip to content

Commit 37e994a

Browse files
authored
Fix database event.data.val() bug (#46)
* Fix bug where event.data.val() was returning array of nulls when database payload had a length property
1 parent 7fc375f commit 37e994a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

spec/providers/database.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ describe('DeltaSnapshot', () => {
137137
populate({ myKey: 'foo', myOtherKey: 'bar' }, { myKey: null });
138138
expect(subject.val()).to.deep.equal({ myOtherKey: 'bar' });
139139
});
140+
141+
// Regression test: .val() was returning array of nulls when there's a property called length (BUG#37683995)
142+
it('should return correct values when data has "length" property', () => {
143+
populate(null, { length: 3, foo: 'bar' });
144+
expect(subject.val()).to.deep.equal({ length: 3, foo: 'bar'});
145+
});
140146
});
141147

142148
describe('#child(): DeltaSnapshot', () => {

src/providers/database.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
248248
let numKeys = 0;
249249
let maxKey = 0;
250250
let allIntegerKeys = true;
251-
_.forEach(node, (childNode, key) => {
251+
for (let key in node) {
252+
if (!node.hasOwnProperty(key)) { continue; }
253+
let childNode = node[key];
252254
obj[key] = this._checkAndConvertToArray(childNode);
253255
numKeys++;
254256
const integerRegExp = /^(0|[1-9]\d*)$/;
@@ -257,7 +259,7 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
257259
} else {
258260
allIntegerKeys = false;
259261
}
260-
});
262+
}
261263

262264
if (allIntegerKeys && maxKey < 2 * numKeys) {
263265
// convert to array.

0 commit comments

Comments
 (0)