[firestore] add support for `DocumentSnapshot.get('dot.notated.field.path')`
This commit is contained in:
parent
590d69ce89
commit
f1e9d28869
|
@ -51,7 +51,8 @@ export default class DataSnapshot {
|
||||||
* @returns {Snapshot}
|
* @returns {Snapshot}
|
||||||
*/
|
*/
|
||||||
child(path: string): DataSnapshot {
|
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);
|
const childRef = this.ref.child(path);
|
||||||
return new DataSnapshot(childRef, {
|
return new DataSnapshot(childRef, {
|
||||||
value,
|
value,
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
import DocumentReference from './DocumentReference';
|
import DocumentReference from './DocumentReference';
|
||||||
import FieldPath from './FieldPath';
|
import FieldPath from './FieldPath';
|
||||||
import Path from './Path';
|
import Path from './Path';
|
||||||
import { isObject } from '../../utils';
|
import { isObject, deepGet } from '../../utils';
|
||||||
import { parseNativeMap } from './utils/serialize';
|
import { parseNativeMap } from './utils/serialize';
|
||||||
|
|
||||||
import type Firestore from './';
|
import type Firestore from './';
|
||||||
|
@ -63,6 +63,6 @@ export default class DocumentSnapshot {
|
||||||
if (fieldPath instanceof FieldPath) {
|
if (fieldPath instanceof FieldPath) {
|
||||||
return extractFieldPathData(this._data, fieldPath._segments);
|
return extractFieldPathData(this._data, fieldPath._segments);
|
||||||
}
|
}
|
||||||
return this._data ? this._data[fieldPath] : undefined;
|
return this._data ? deepGet(this._data, fieldPath, '.') : undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ export function deepGet(
|
||||||
|
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
const key = keys[i++];
|
const key = keys[i++];
|
||||||
if (!tmp || !hasOwnProperty.call(tmp, key)) return null;
|
if (!tmp || !hasOwnProperty.call(tmp, key)) return undefined;
|
||||||
tmp = tmp[key];
|
tmp = tmp[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue