[英]Firestore Getting documents id from collection
我正在尝试使用 id 检索我的文档,但无法弄清楚。
目前我检索我的文件是这样的:
const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();
我确实完美地获得了我的文档列表,但是它们没有文档 ID。
如何为每个文档检索它?
对于 angular 8 和 Firebase 6,您可以使用选项 id 字段
getAllDocs() {
const ref = this.db.collection('items');
return ref.valueChanges({idField: 'customIdName'});
}
这会使用指定的键 (customIdName) 在对象上添加文档的 Id
要获取集合中文档的 id,您必须使用snapshotChanges()
this.shirtCollection = afs.collection<Shirt>('shirts');
// .snapshotChanges() returns a DocumentChangeAction[], which contains
// a lot of information about "what happened" with each change. If you want to
// get the data and the id use the map operator.
this.shirts = this.shirtCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Shirt;
const id = a.payload.doc.id;
return { id, ...data };
});
});
我终于找到了解决方案。 维克多非常了解文档数据。
const racesCollection: AngularFirestoreCollection<Race>;
return racesCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Race;
data.id = a.payload.doc.id;
return data;
});
});
ValueChanges() 不包含元数据,因此当我们需要文档 id 时我们必须使用 SnapshotChanges() 然后按照此处的说明正确映射它https://github.com/angular/angularfire2/blob/master/docs/firestore/集合.md
对于 angular6+
this.shirtCollection = afs.collection<Shirt>('shirts');
this.shirts = this.shirtCollection.snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Shirt;
const id = a.payload.doc.id;
return { id, ...data };
});
})
);
doc.id
获取 UID。
结合一个对象的其余数据,如下所示:
Object.assign({ uid: doc.id }, doc.data())
由于您使用的是 angularFire,因此如果您要为实现返回默认的 firebase 方法,则没有任何意义。 AngularFire 本身已经实现了适当的机制。 只需要使用它。
angularFire 的valueChanges()
方法提供了一个重载,用于通过简单地将一个对象作为参数添加到该方法来获取集合的每个文档的 ID。
valueChanges({ idField: 'id' })
这里的 'idField' 必须和原来的一样。 'id' 可以是您希望调用文档 ID 的任何内容。
然后返回数组上的每个文档对象将如下所示。
{
field1 = <field1 value>,
field2 = <field2 value>,
..
id = 'whatEverTheDocumentIdWas'
}
然后,您可以通过引用您命名的字段轻松获取文档 ID。
AngularFire 5.2.0
在数据库中添加文档之前可以获取ID:
var idBefore = this.afs.createId();
console.log(idBefore);
对于文档引用,而不是集合,您需要:
// when you know the 'id'
this.afs.doc(`items/${id}`)
.snapshotChanges().pipe(
map((doc: any) => {
const data = doc.payload.data();
const id = doc.payload.id;
return { id, ...data };
});
作为.valueChanges({ idField: 'id'});
不会在这里工作。 我认为它没有实现,因为通常你通过 id 搜索文档......
试过这个!
colWithIds$<T>(ref: CollectionPredicate<T>, queryFn?): Observable<any[]> {
return this.col(ref, queryFn).snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
});
}));
}
但是我遇到这个错误
[ts]传播类型只能从对象类型创建。 const数据:T
我试过这个
return this.db.collection('items').snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Item;
data.id = a.payload.doc.id;
data.$key = a.payload.doc.id;
return data;
});
})
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.