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