Skip to content

Commit f067dbd

Browse files
markgohodavideast
authored andcommitted
docs: fix object spread typing and mis-named variables (#1188)
There was an issue with typing (I think) using the current way of returning a new object with the doc.id included. This method ensures that what gets returned is properly typed. This method was used on two other examples (`.stateChanges()` and `auditTrail()`) so those were updated as well. I also found a handful of mis-named types (assume they were missed from copy/pasting) and so I updated those as well.
1 parent 243184e commit f067dbd

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

docs/firestore/collections.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ import { Observable } from 'rxjs/Observable';
139139
import 'rxjs/add/operator/map';
140140

141141
export interface Shirt { name: string; price: number; }
142+
export interface ShirtId extends Shirt { id: string; }
142143

143144
@Component({
144145
selector: 'app-root',
@@ -152,14 +153,18 @@ export interface Shirt { name: string; price: number; }
152153
})
153154
export class AppComponent {
154155
private shirtCollection: AngularFirestoreCollection<Shirt>;
155-
shirts: Observable<Shirt[]>;
156+
shirts: Observable<ShirtId[]>;
156157
constructor(private readonly afs: AngularFirestore) {
157158
this.shirtCollection = afs.collection<Shirt>('shirts');
158159
// .snapshotChanges() returns a DocumentChangeAction[], which contains
159160
// a lot of information about "what happened" with each change. If you want to
160161
// get the data and the id use the map operator.
161162
this.shirts = this.shirtCollection.snapshotChanges().map(actions => {
162-
return actions.map(a => ({ id: a.payload.doc.id(), ...a.payload.doc.data() }))
163+
return actions.map(a => {
164+
const data = a.payload.doc.data() as Shirt;
165+
const id = a.payload.doc.id;
166+
return { id, ...data };
167+
});
163168
});
164169
}
165170
}
@@ -179,6 +184,7 @@ import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/fires
179184
import { Observable } from 'rxjs/Observable';
180185

181186
export interface AccountDeposit { description: string; amount: number; }
187+
export interface AccountDepoistId extends AccountDeposit { id: string; }
182188

183189
@Component({
184190
selector: 'app-root',
@@ -191,13 +197,17 @@ export interface AccountDeposit { description: string; amount: number; }
191197
`
192198
})
193199
export class AppComponent {
194-
private depositCollection: AngularFirestoreCollection<Shirt>;
195-
deposits: Observable<Shirt[]>;
200+
private depositCollection: AngularFirestoreCollection<AccountDeposit>;
201+
deposits: Observable<AccountDepositId[]>;
196202
constructor(private readonly afs: AngularFirestore) {
197203
this.depositCollection = afs.collection<AccountDeposit>('deposits');
198204
this.deposits = this.shirtCollection.stateChanges(['added'])
199205
.map(actions => {
200-
return actions.map(a => ({ id: a.payload.doc.id(), ...a.payload.doc.data() }))
206+
return actions.map(a => {
207+
const data = a.payload.doc.data() as AccountDeposit;
208+
const id = a.payload.doc.id;
209+
return { id, ...data };
210+
})
201211
});
202212
}
203213
}
@@ -219,6 +229,7 @@ import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/fires
219229
import { Observable } from 'rxjs/Observable';
220230

221231
export interface AccountLogItem { description: string; amount: number; }
232+
export interface AccountLogItemId extends AccountLogItem { id: string; }
222233

223234
@Component({
224235
selector: 'app-root',
@@ -232,12 +243,16 @@ export interface AccountLogItem { description: string; amount: number; }
232243
})
233244
export class AppComponent {
234245
private accountLogCollection: AngularFirestoreCollection<AccountLogItem>;
235-
accountLogs: Observable<AccountLogItem[]>;
246+
accountLogs: Observable<AccountLogItemId[]>;
236247
constructor(private readonly afs: AngularFirestore) {
237-
this.accountLogCollection = afs.collection<AccountDeposit>('accountLog');
248+
this.accountLogCollection = afs.collection<AccountLogItem>('accountLog');
238249
this.accountLogs = this.shirtCollection.auditTrail()
239250
.map(actions => {
240-
return actions.map(a => ({ id: a.payload.doc.id(), ...a.payload.doc.data() }))
251+
return actions.map(a => {
252+
const data = a.payload.doc.data() as AccountLogItem;
253+
const id = a.payload.doc.id;
254+
return { id, ...data };
255+
})
241256
});
242257
}
243258
}
@@ -305,4 +320,4 @@ To add a new document to a collection with a generated id use the `add()` method
305320

306321
To retrieve, update, or delete an individual document you can use the `doc()` method. This method returns an `AngularFirestoreDocument`, which provides methods for streaming, updating, and deleting. [See Using Documents with AngularFirestore for more information on how to use documents](documents.md).
307322

308-
### [Next Step: Querying Collections in AngularFirestore](querying-collections.md)
323+
### [Next Step: Querying Collections in AngularFirestore](querying-collections.md)

0 commit comments

Comments
 (0)