下面的查询根据用户 ID 返回一组数据。我该如何确保它返回查询中每个文档的 uid。
getWebsitesByUserId(id) {
return this.afs.collection('websites', ref => ref.where('createdBy', '==', id)).valueChanges();
}
我知道它涉及这样的事情:
return this.afs.collection('websites').doc(id).snapshotChanges().pipe(map(action => {
const data = action.payload.data();
const uid = action.payload.id;
return {uid, ...data};
}));
只是不确定如何在查询中实现。
根据AngularFire Repo 上的这个拉取请求,现在有一个选项可以将字符串传递给集合引用上的 valueChanges 方法。
此字符串将是保存文档 ID 的属性的名称。
例如:
collectionRef.valueChanges('myIdKey').subscribe()
会发出:
emits [ { myIdKey: 'MrfFpRBfWLTd7LqiTt9u', ...data }, ... ]
所以在你的情况下,我想它会是这样的:
getWebsitesByUserId(id) {
return this.afs.collection('websites', ref => ref.where('createdBy', '==', id)).valueChanges('uid');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句