[英]Angular Fire fetching related data from same collection
I´m facing a strange behavior with a query on my firestore project.我在查询我的 firestore 项目时遇到了一个奇怪的行为。 One user of the project is like the owner of the system, we call him the producer.项目的一位用户就像系统的所有者,我们称他为生产者。 And the other users
, can belong to one producer
, so I have a collection of users
, where I have a field called producer
, with the producer ID
, like below.而其他users
,可以属于一个producer
,所以我有一个users
集合,其中我有一个名为producer
的字段,带有producer ID
,如下所示。
{user: {id: '...', producer: '...', ...}
* Translate producer to produtor. *将制作人翻译为制作人。
I have a functionality called a transfer, where I can transfer cash from one user account to another, but only within the users of the same producer.我有一个称为转移的功能,我可以将现金从一个用户帐户转移到另一个帐户,但仅限于同一生产者的用户内部。 So I need to fetch all the users where the producer is the same as the logged user, on this collection, here is my function:所以我需要获取生产者与登录用户相同的所有用户,在这个集合上,这是我的 function:
constructor(
private afAuth: AngularFireAuth,
private afs: AngularFirestore
) { }
fetchUsers() {
return this.afAuth.authState.pipe(
switchMap( user => { // Here I have the logged user
return this.afs.doc(`users/${user.uid}`).valueChanges() // Here I need his details on users collection
}),
first(),
switchMap( (user: any) => {
return this.afs.collection('users',
ref => ref.where('producer', '==', user.producer) // Now I want the other users from same producer
).valueChanges()
})
)
}
But it does not work, the only user on the result is the user calling the function.但它不起作用,结果上唯一的用户是调用 function 的用户。 Is it a limitation of the Angular Fire or maybe I´m doing it in the wrong way?这是 Angular Fire 的限制还是我做错了?
I think it´s maybe a bug, because I could workaround this using get on document and then on collection:我认为这可能是一个错误,因为我可以使用 get on document 然后 on collection 来解决这个问题:
return this.afAuth.authState.pipe(
switchMap( user => {
return this.afs.doc(`users/${user.uid}`).get()
}),
first(),
map( usuario => {
return usuario.data()
}),
switchMap( (usuario: any) => {
return this.afs.collection('users', ref => ref.where('produtor', '==', usuario.produtor)).get()
}),
first(),
map( data => {
let users = []
data.docs.map( doc => {
users.push(doc.data())
})
return users
})
)
This way, it returns all the users where the producer, produtor , is the same of logged in user.这样,它返回生产者produtor与登录用户相同的所有用户。 I´ll appreciate if someone could explain me why.如果有人能解释我为什么,我将不胜感激。
Looks like your OR query logic is incorrect.看起来您的 OR 查询逻辑不正确。
return this.afs.collection('users',
ref => ref.where('producer', 'in', [user.producer, user.uid])
).valueChanges()
https://firebase.google.com/docs/firestore/query-data/queries https://firebase.google.com/docs/firestore/query-data/queries
In fact it appears you do not need an OR condition at all.事实上,您似乎根本不需要 OR 条件。 Why would user.producer
be set to two different types?为什么将user.producer
设置为两种不同的类型?
ie: if user.producer
is always set to a producer ID, then you can have just:即:如果user.producer
始终设置为生产者 ID,那么您可以只拥有:
return this.afs.collection('users',
ref => ref.where('producer', '==', user.uid)
).valueChanges()
When you call.valueChanges() in Angular Fire, it returns an observable.当你在 Angular Fire 中调用 .valueChanges() 时,它会返回一个 observable。
Observables open up a continuous channel of communication in which multiple values of data can be emitted over time. Observables 开辟了一个连续的通信渠道,随着时间的推移,可以在其中发出多个数据值。
So we need to get a value from an Observable and for that we need to subscribe to it.所以我们需要从 Observable 中获取一个值,为此我们需要订阅它。 You could call the document with for valueChanges and attach a pipe(take(1)) as following, but get() is pretty handy.您可以使用 for valueChanges 调用文档并附加一个 pipe(take(1)),如下所示,但 get() 非常方便。
this.afs.doc(`users/${user.uid}`).valueChanges()
.pipe(take(1))
.subscribe(v => {
console.log(v);
});
Please let me know if you need any further clarification.如果您需要任何进一步的说明,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.