在 Firebase 查询中返回 UID
Returning UID in Firebase query
下面的查询 returns 我是基于用户 ID 的数据集合。我如何确保它 returns 查询中每个文档的 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 上的 this pull request,现在有一个选项,您可以将字符串传递给集合引用的 valueChanges 方法。
此字符串将是 属性 的名称,它将保存文档的 ID。
例如:
collectionRef.valueChanges('myIdKey').subscribe()
会发出:
emits [ { myIdKey: 'MrfFpRBfWLTd7LqiTt9u', ...data }, ... ]
所以在你的情况下我猜它会是这样的:
getWebsitesByUserId(id) {
return this.afs.collection('websites', ref => ref.where('createdBy', '==', id)).valueChanges('uid');
}
下面的查询 returns 我是基于用户 ID 的数据集合。我如何确保它 returns 查询中每个文档的 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 上的 this pull request,现在有一个选项,您可以将字符串传递给集合引用的 valueChanges 方法。
此字符串将是 属性 的名称,它将保存文档的 ID。
例如:
collectionRef.valueChanges('myIdKey').subscribe()
会发出:
emits [ { myIdKey: 'MrfFpRBfWLTd7LqiTt9u', ...data }, ... ]
所以在你的情况下我猜它会是这样的:
getWebsitesByUserId(id) {
return this.afs.collection('websites', ref => ref.where('createdBy', '==', id)).valueChanges('uid');
}