简体   繁体   English

getDocuments() 函数多次获取相同的数据

[英]getDocuments() function getting the same data more than one time

I am migrating from relational DB to NOSQL, and I already have read the entire Firebase Documentation, and now I am hands on to a study project to learn more about it.我正在从关系数据库迁移到 NOSQL,并且我已经阅读了整个 Firebase 文档,现在我正在着手研究项目以了解更多信息。

What I am trying to do is: I have an user_profile where each user created by Auth will get one document.我想做的是:我有一个user_profile ,其中Auth创建的每个用户都将获得一个文档。

Inside this document I have an array called groups that shows all groups this user has joined.在本文档中,我有一个名为groups的数组,它显示了该用户加入的所有组。

I need to retrieve a list of this groups according to the user logged in.我需要根据登录的用户检索该组的列表。

*这是我目前的数据库*

我的数据库

Then I am using getDocuments() to retrieve this information from the DB.然后我使用 getDocuments() 从数据库中检索此信息。

What I need to get is a array of string with the group_id (as per second pic).我需要得到的是一个带有 group_id 的字符串数组(根据第二张图片)。

The my current code is retrieving the information I need, but it get all the ids as one object.我当前的代码正在检索我需要的信息,但它将所有 ID 作为一个对象获取。 So if I have 4 groups, it will retrieve 4 objects containing the all 4 groups the user has joined.因此,如果我有 4 个组,它将检索 4 个对象,其中包含用户已加入的所有 4 个组。

func getGroups() -> [String] {

    let currentUser = Auth.auth().currentUser?.uid
    let db = Firestore.firestore()
    var groups = [""]
    var groupsArray = [""]

        db.collection(K.Collections.userProfile)
            .whereField(K.DBFields.UserProfile.userId, isEqualTo: currentUser!)
            .getDocuments { (snapshot, error) in
            if let error = error  {
                print(error)
            } else {

                for document in snapshot!.documents {
                    groups = (document.get("groups")) as! [String]
                    //groupsArray.append(groups)
                    print("Group ID: \(groups)")
                    }
            }
        }
return groups
}

And the result I am getting is the one below:我得到的结果如下:

roup ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] roup ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm ", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"] 组 ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]

I have tried so many different approaches to try to fetch the correct data, but hasn't work.我尝试了很多不同的方法来尝试获取正确的数据,但没有奏效。

Once again, I just want to get this array as result: var groups = [ "8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]再一次,我只想得到这个数组作为结果:var groups = [ "8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]

Thanks a lot Leonardo D'Amato非常感谢莱昂纳多达马托

You're retrieving the values from groups as [String] (string array).您正在以[String] (字符串数组)的形式从groups中检索值。 But then you print that string array as a single value.但是随后您将该字符串数组打印为单个值。

If you want to access the individual elements of the [String] in Swift, you can loop over it:如果你想在 Swift 中访问[String]的各个元素,你可以遍历它:

groups = (document.get("groups")) as! [String]
for group in groups {
    print("Group ID \(group).")
}

Also see the Swift documentation on accessing array values .另请参阅有关 访问数组值的 Swift 文档。

I don't have much idea of Swift but in "node" there were different results when I tried to use for in place of map You should use the following:我对 Swift 不太了解,但是在“节点”中,当我尝试使用 for 代替 map 时出现了不同的结果你应该使用以下内容:

let dataDescription = snapshot.data().map(String.init(describing:))??让 dataDescription = snapshot.data().map(String.init(describing:))?? "nil" “零”

print("Document data: (dataDescription)") print("文档数据:(dataDescription)")

Shown here 显示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getdocuments function 不停 - getdocuments function not stopping 有没有办法将多个云 function 附加到 Firestore 中的特定集合? - Is there a way to attach more than one cloud function to a specific collection in Firestore? 获取更多关于 Gremlin 遍历的数据而不仅仅是节点 ID? - Getting more data on Gremlin traversal than just node id? Google Cloud 中的 EventArc 一次多次请求 Cloud Run Service 触发器 - EventArc in Google Cloud Request the Cloud Run Service more than Once in One Time Trigger GCP 数据流批处理作业 - 防止工作人员在批处理作业中一次运行多个元素 - GCP Dataflow Batch jobs - Preventing workers from running more than one element at a time in a batch job 如何在bigquery中获取过期的表数据,如果过期时间超过两天? - How to get expired table data in bigquery, If the expired time is more than two days? Nginx 比 uwsgi 花费更多的时间 - Nginx taking way more time than uwsgi Azure Stream 分析 - 如何在 Azure SQL db 中保存多个物联网设备的数据 - Azure Stream Analytics - How to save data for more than one IoT device in Azure SQL db Azure Data Explorer 创建物化视图时如何添加多个维表 - how to add more than one dimension table while creating a materialized view in Azure Data Explorer 在 GCP 上添加多个组织域 - Adding more than one organisation domain on GCP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM