Я использую AngularFire2 в службе для получения данных из коллекции Firestore. Код выглядит примерно так:
this.db.collection('organizations')
.valueChanges()
.pipe(first())
.toPromise()
.next(organization => console.info(organization));
Консоль регистрирует объект организации точно так, как ожидалось. Но в этом объекте отсутствует идентификатор документа Firestore.
Поэтому мне интересно, можно ли что-то сделать, чтобы получить идентификатор как часть этого запроса...





вы можете использовать снимок примерно так:
private getOrganizations(whereClause: any): any {
return this.db.collection('organizations')
.snapshotChanges()
.pipe(
map((docs: any) => {
return docs.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return {id, ...data};
});
})
);
}
Для получения более подробной информации о snapshotChanges проверьте это:
https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md#snapshotchanges
snapshotChanges()
What is it? - Returns an Observable of data as a DocumentChangeAction.
Why would you use it? - When you need the document data but also want to keep around metadata. This metadata provides you the underyling DocumentReference and document id. Having the document's id around makes it easier to use data manipulation methods. This method gives you more horsepower with other Angular integrations such as ngrx, forms, and animations due to the type property. The type property on each DocumentChangeAction is useful for ngrx reducers, form states, and animation states.What is it? - Returns an Observable of data as a
Спасибо Надир, похоже на то!