AngularFire2 查询不返回文档 ID
AngularFire2 query not returning document ID
我在服务中使用 AngularFire2 从 Firestore 集合中获取数据。代码看起来像这样:
this.db.collection('organizations')
.valueChanges()
.pipe(first())
.toPromise()
.next(organization => console.log(organization));
控制台正在按预期记录组织对象。但是该对象缺少 Firestore 文档的 ID。
所以我想知道是否可以做些什么来获取 ID 作为该查询的一部分...
您可以像这样使用快照:
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
我在服务中使用 AngularFire2 从 Firestore 集合中获取数据。代码看起来像这样:
this.db.collection('organizations')
.valueChanges()
.pipe(first())
.toPromise()
.next(organization => console.log(organization));
控制台正在按预期记录组织对象。但是该对象缺少 Firestore 文档的 ID。
所以我想知道是否可以做些什么来获取 ID 作为该查询的一部分...
您可以像这样使用快照:
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