我正在尝试获取我收藏的文档的文档 ID。 为此,我创建了这个:
this.afs.collection("Blogs").valueChanges().map(document => {
return document.where('title','==','test')(a => {
const data = a.payload.doc.data();//Here is your content
const id = a.payload.doc.id;//Here is the key of your document
console.log('documentsId', id)
});
});
}
不幸的是它不起作用,但我也没有收到错误。
最佳答案
您使用的代码段仅适用于snapshotChanges
。 <强> valueChanges
如果没有提供 id,则仅返回对象数据。提供 id 作为参数应该可以在 latest version 中工作。根据文档,angularfire
。
但是对于你的情况来说这是行不通的,你不知道我假设的id。因此,就像文档建议的那样,如果您需要元数据,请使用 snapShotChanges
另外,我假设您正在使用带有管道运算符的 rxjs 版本,如果是这样,请使用 pipe
。另外,您的代码看起来与 ref
有点不一样。尝试:
import { map } from 'rxjs/operators';
// ....
return this.afs.collection('Blogs', ref => ref.where('title','==','test'))
.snapshotChanges()
.pipe(
map(docs => docs.map(a => {
const data = a.payload.doc.data(); // Here is your content
const id = a.payload.doc.id; // Here is the key of your document
console.log('documentsId', id);
return {id: id, ...data }
})
)
)
并且记得在组件中订阅这个函数。
关于javascript - Angular 无法获取文档 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57372195/