如何基于Angular中使用Firestore的另一个查询的返回进行查询? [英] How do I make a query based on the return of another query in Angular with Firestore?

查看:105
本文介绍了如何基于Angular中使用Firestore的另一个查询的返回进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个集合中获取一些ID,然后尝试使用这些ID中的每一个从另一个集合中获取更多数据.到目前为止,这是我所做的:

Im trying to get some Ids from a collection, and then trying to use each of these Ids to get more data from another collection. Here is what I got done so far:

基本上,我有一个博客应用程序,其中多个用户将可以发布文章.并说文章有分类.问题是,当用户选择要过滤的类别时,它将带给我所有在该类别中具有文章的用户(而不是带入所选类别的所有文章).

Basically, I have blog app in which multiple users will be able to post an article. And said article has categories. The thing is, when a user selects a category to filter, it will bring me all USERS that have an article in said category (instead of bringing all articles of the selected category).

所以,在Firestore中,我有:

So, in Firestore I have:

categories/{categoryId}/users/{userId}

尽管{userId}中包含更多信息(例如此{categoryId}中的文章ID),但我现在仅对userId本身感兴趣.

Although the {userId} has more info inside it (like the articles ids in this {categoryId}), I'm just interested in the userId itself for now.

我想做的是使用上述路径中恢复的userId,并使用它从以下路径中获取用户数据:

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存储在数组中,然后使用所述数组中的forEach循环向Firestore发出请求吗?我应该在 categories/{categoryId}/users/{userId} 上复制用户数据吗?也许改变我的Firestore数据结构?也许还有其他方法可以在Angular中做到这一点?

And here's where I'm stuck and not sure how to proceed next.. 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?

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,但请多多帮助!

I apologise for such a simple question. I'm still getting the hang of Angular and Firestore, but appreciate any help!

谢谢.

更新

我做了以下事情:

.subscribe(ids => {
    ids.map(id => {
        this.afs.collection('users').doc<User>(id).valueChanges().map(user => {
            return user;
        }).subscribe(user => {
            console.log(user); //Got it!
        })
    })
})

推荐答案

我想做的是使用上述路径中恢复的userId,并使用它从以下路径中获取用户数据:users/{userId}

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创建一个新引用,并像这样获取每个用户的详细信息:

You're almost there. What you need to do, is to use the id of the user that you get from the database inside a new reference. So in this case, you need to use nested listeners. So inside your first callback, just create a new reference using the user id and get the details for each user like this:

return docChangeActionArray.map(value2 => {
    return value2.payload.doc.id;

    this.afs.collection<User>('users').doc(value2.payload.doc.id).snapshotChanges().map(/* ... */);
})

请注意,Cloud Firestore中的嵌套侦听器没有错.

Note, that is nothing wrong about nested listeners in Cloud Firestore.

我应该将ID存储在数组中,然后使用所述数组中的forEach循环向Firestore发出请求吗?

Should I store the ids in a array, and then make requests to Firestore using a forEach loop in said array?

没有必要.

我是否应该在category/{categoryId}/users/{userId}上复制用户数据?也许改变我的Firestore数据结构?

Should I duplicate the user data on categories/{categoryId}/users/{userId}? Perhaps change my Firestore data structure?

这取决于您的应用程序的用例.多少复制数据与额外的数据库调用最适合您,这实际上取决于项目的要求.有关更多信息,请参阅此

It depends on use-case of your app. How much duplication data versus extra database calls is optimal for you, it really depends on your project's requirements. For more informations, please see my answer from this post.

也许在Angular中有另一种方式可以做到这一点?

Or perhaps theres a different way to do it in Angular?

在这种情况下,我看不到其他方法.鉴于以上帖子中的信息,您可以自行决定哪种解决方案更适合您.

I cannot see another approach in this case. It's up to you to decide which solution might be better for you, given the information in the answer from the post above.

这篇关于如何基于Angular中使用Firestore的另一个查询的返回进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆