[firestore] add support for `DocumentSnapshot.get('dot.notated.field.path')`

This commit is contained in:
Salakar 2018-04-12 12:30:41 +01:00
parent 590d69ce89
commit f1e9d28869
3 changed files with 5 additions and 4 deletions

View File

@ -51,7 +51,8 @@ export default class DataSnapshot {
* @returns {Snapshot}
*/
child(path: string): DataSnapshot {
const value = deepGet(this._value, path);
let value = deepGet(this._value, path);
if (value === undefined) value = null;
const childRef = this.ref.child(path);
return new DataSnapshot(childRef, {
value,

View File

@ -5,7 +5,7 @@
import DocumentReference from './DocumentReference';
import FieldPath from './FieldPath';
import Path from './Path';
import { isObject } from '../../utils';
import { isObject, deepGet } from '../../utils';
import { parseNativeMap } from './utils/serialize';
import type Firestore from './';
@ -63,6 +63,6 @@ export default class DocumentSnapshot {
if (fieldPath instanceof FieldPath) {
return extractFieldPathData(this._data, fieldPath._segments);
}
return this._data ? this._data[fieldPath] : undefined;
return this._data ? deepGet(this._data, fieldPath, '.') : undefined;
}
}

View File

@ -33,7 +33,7 @@ export function deepGet(
while (i < len) {
const key = keys[i++];
if (!tmp || !hasOwnProperty.call(tmp, key)) return null;
if (!tmp || !hasOwnProperty.call(tmp, key)) return undefined;
tmp = tmp[key];
}