Leonardo D'Amato
Leonardo D'Amato

Reputation: 33

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.

What I am trying to do is: I have an user_profile where each user created by Auth will get one document.

Inside this document I have an array called groups that shows all groups this user has joined.

I need to retrieve a list of this groups according to the user logged in.

*This is my current DB*

My DB

Then I am using getDocuments() to retrieve this information from the DB.

What I need to get is a array of string with the group_id (as per second pic).

The my current code is retrieving the information I need, but it get all the ids as one object. So if I have 4 groups, it will retrieve 4 objects containing the all 4 groups the user has joined.

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"]

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"]

Thanks a lot Leonardo D'Amato

Upvotes: 0

Views: 206

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You're retrieving the values from groups as [String] (string array). 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:

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

Also see the Swift documentation on accessing array values.

Upvotes: 1

Saurav Suman
Saurav Suman

Reputation: 41

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:

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

print("Document data: (dataDescription)")

Shown here

Upvotes: 0

Related Questions