如何使用 Firestore 根据 Angular 中另一个查询的 return 进行查询?
How do I make a query based on the return of another query in Angular with Firestore?
我正在尝试从一个集合中获取一些 ID,然后尝试使用这些 ID 中的每一个从另一个集合中获取更多数据。这是我到目前为止完成的工作:
基本上,我有一个博客应用程序,多个用户可以在其中 post 一篇文章。并且说文章有类别。问题是,当用户选择要过滤的类别时,它将为我带来所有在该类别中有文章的用户(而不是带来所选类别的所有文章)。
因此,在 Firestore 中我有:
categories/{categoryId}/users/{userId}
尽管 {userId} 中包含更多信息(例如此 {categoryId} 中的文章 ID),但我现在只对 userId 本身感兴趣。
我想做的是使用从上述路径恢复的userIds,并使用它从以下路径获取用户数据:
users/{userId}
这就是我卡住的地方,不确定下一步该怎么做。我是否应该将 ID 存储在一个数组中,然后使用该数组中的 forEach 循环向 Firestore 发出请求?我应该在 categories/{categoryId}/users/{userId} 上复制用户数据吗?也许改变我的 Firestore 数据结构?或者在 Angular?
中可能有不同的方法
this.afs.collection<Category>('categories').doc(category.name).collection<User>('users')
.snapshotChanges().map(docChangeActionArray => {
return docChangeActionArray.map(value2 => {
return value2.payload.doc.id;
})
}).subscribe(ids => {
//WHAT TO DO HERE?
})
对于这么简单的问题,我深表歉意。我仍在掌握 Angular 和 Firestore,但感谢您的帮助!
谢谢。
更新
我做了以下事情:
.subscribe(ids => {
ids.map(id => {
this.afs.collection('users').doc<User>(id).valueChanges().map(user => {
return user;
}).subscribe(user => {
console.log(user); //Got it!
})
})
})
What I want to do is to use the recovered userIds from the above path, and use it to get the user data from the following path: users/{userId}
你快到了。您需要做的是使用您从数据库中获取的用户的 id
在一个新的引用中。所以在这种情况下,你需要使用嵌套的监听器。因此,在您的第一个回调中,只需使用用户 ID 创建一个新引用并获取每个用户的详细信息,如下所示:
return docChangeActionArray.map(value2 => {
return value2.payload.doc.id;
this.afs.collection<User>('users').doc(value2.payload.doc.id).snapshotChanges().map(/* ... */);
})
请注意,Cloud Firestore 中的嵌套侦听器并没有错。
Should I store the ids in a array, and then make requests to Firestore using a forEach loop in said array?
没有这个必要。
Should I duplicate the user data on categories/{categoryId}/users/{userId}? Perhaps change my Firestore data structure?
这取决于您的应用程序的用例。多少重复数据与额外的数据库调用对您来说是最佳的,这实际上取决于您的项目要求。有关更多信息,请参阅我在此 .
中的回答
Or perhaps theres a different way to do it in Angular?
在这种情况下,我看不到其他方法。根据上面 post 的答案中的信息,由您决定哪种解决方案可能更适合您。
我正在尝试从一个集合中获取一些 ID,然后尝试使用这些 ID 中的每一个从另一个集合中获取更多数据。这是我到目前为止完成的工作:
基本上,我有一个博客应用程序,多个用户可以在其中 post 一篇文章。并且说文章有类别。问题是,当用户选择要过滤的类别时,它将为我带来所有在该类别中有文章的用户(而不是带来所选类别的所有文章)。
因此,在 Firestore 中我有:
categories/{categoryId}/users/{userId}
尽管 {userId} 中包含更多信息(例如此 {categoryId} 中的文章 ID),但我现在只对 userId 本身感兴趣。
我想做的是使用从上述路径恢复的userIds,并使用它从以下路径获取用户数据:
users/{userId}
这就是我卡住的地方,不确定下一步该怎么做。我是否应该将 ID 存储在一个数组中,然后使用该数组中的 forEach 循环向 Firestore 发出请求?我应该在 categories/{categoryId}/users/{userId} 上复制用户数据吗?也许改变我的 Firestore 数据结构?或者在 Angular?
中可能有不同的方法this.afs.collection<Category>('categories').doc(category.name).collection<User>('users')
.snapshotChanges().map(docChangeActionArray => {
return docChangeActionArray.map(value2 => {
return value2.payload.doc.id;
})
}).subscribe(ids => {
//WHAT TO DO HERE?
})
对于这么简单的问题,我深表歉意。我仍在掌握 Angular 和 Firestore,但感谢您的帮助!
谢谢。
更新
我做了以下事情:
.subscribe(ids => {
ids.map(id => {
this.afs.collection('users').doc<User>(id).valueChanges().map(user => {
return user;
}).subscribe(user => {
console.log(user); //Got it!
})
})
})
What I want to do is to use the recovered userIds from the above path, and use it to get the user data from the following path: users/{userId}
你快到了。您需要做的是使用您从数据库中获取的用户的 id
在一个新的引用中。所以在这种情况下,你需要使用嵌套的监听器。因此,在您的第一个回调中,只需使用用户 ID 创建一个新引用并获取每个用户的详细信息,如下所示:
return docChangeActionArray.map(value2 => {
return value2.payload.doc.id;
this.afs.collection<User>('users').doc(value2.payload.doc.id).snapshotChanges().map(/* ... */);
})
请注意,Cloud Firestore 中的嵌套侦听器并没有错。
Should I store the ids in a array, and then make requests to Firestore using a forEach loop in said array?
没有这个必要。
Should I duplicate the user data on categories/{categoryId}/users/{userId}? Perhaps change my Firestore data structure?
这取决于您的应用程序的用例。多少重复数据与额外的数据库调用对您来说是最佳的,这实际上取决于您的项目要求。有关更多信息,请参阅我在此
Or perhaps theres a different way to do it in Angular?
在这种情况下,我看不到其他方法。根据上面 post 的答案中的信息,由您决定哪种解决方案可能更适合您。