From 9b1873e2905cf3f921e4d687c159ba7d9ee71fd0 Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sat, 13 Mar 2021 19:31:57 -0500 Subject: [PATCH] fix(firestore): do not return idField on valueChanges if document does not exists --- src/firestore/document/document.spec.ts | 21 ++++----------------- src/firestore/document/document.ts | 2 +- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/firestore/document/document.spec.ts b/src/firestore/document/document.spec.ts index 9b5d93dcc..2c2fd50fb 100644 --- a/src/firestore/document/document.spec.ts +++ b/src/firestore/document/document.spec.ts @@ -36,8 +36,8 @@ describe('AngularFirestoreDocument', () => { it('should get unwrapped snapshot', async (done: any) => { const randomCollectionName = afs.firestore.collection('a').doc().id; - const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as firebase.firestore.DocumentReference; - const stock = new AngularFirestoreDocument(ref, afs); + const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference; + const stock = new AngularFirestoreDocument(ref, afs); await stock.set(FAKE_STOCK_DATA); const obs$ = stock.valueChanges(); obs$.pipe(take(1)).subscribe(async data => { @@ -46,10 +46,9 @@ describe('AngularFirestoreDocument', () => { }); }); - /* TODO(jamesdaniels): test is flaking, look into this it('should optionally map the doc ID to the emitted data object', async (done: any) => { const randomCollectionName = afs.firestore.collection('a').doc().id; - const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`); + const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference; const stock = new AngularFirestoreDocument(ref, afs); await stock.set(FAKE_STOCK_DATA); const idField = 'myCustomID'; @@ -59,7 +58,7 @@ describe('AngularFirestoreDocument', () => { expect(data).toEqual(jasmine.objectContaining(FAKE_STOCK_DATA)); stock.delete().then(done).catch(done.fail); }); - });*/ + }); }); @@ -81,18 +80,6 @@ describe('AngularFirestoreDocument', () => { }); }); - it('should get unwrapped snapshot', async (done: any) => { - const randomCollectionName = afs.firestore.collection('a').doc().id; - const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference; - const stock = new AngularFirestoreDocument(ref, afs); - await stock.set(FAKE_STOCK_DATA); - const obs$ = stock.valueChanges(); - obs$.pipe(take(1)).subscribe(async data => { - expect(data).toEqual(FAKE_STOCK_DATA); - stock.delete().then(done).catch(done.fail); - }); - }); - }); }); diff --git a/src/firestore/document/document.ts b/src/firestore/document/document.ts index f0a7ab992..7515aee2c 100644 --- a/src/firestore/document/document.ts +++ b/src/firestore/document/document.ts @@ -88,7 +88,7 @@ export class AngularFirestoreDocument { valueChanges(options: { idField?: K } = {}): Observable { return this.snapshotChanges().pipe( map(({ payload }) => - options.idField ? { + options.idField && payload.exists ? { ...payload.data(), ...{ [options.idField]: payload.id } } as T & { [T in K]: string } : payload.data()