Home > OS >  firebase users collection has document of users with strange ID
firebase users collection has document of users with strange ID

Time:02-22

I;m new to firebase, I've users collection with documents, but the ID's are not know for me, I don't know how to access them, I tried with uid but those ID's doesn't look like uids therefore It didn't work

firebase Picture

as you can see in the screenshot above, documents ids are different from users uids my target is to access user, after that document and add a new collection ( of course not in all users, the registered one.

any ideas how to access them?

            // create user
            Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
                // check for errors
                
                if let err = err {
                    // error occuried
                    showError("error while creating user")
                }else {
                    // user was created succesfully,store name and vaccine
                    let db = Firestore.firestore()
                    
                    db.collection("users").addDocument(data: ["username":self.usernameLabelRegister.text ,"vaccine":self.chosenVaccine,"nationality":self.chosenLocation, "uid":result!.user.uid,"profileImageUrl":RegisterViewController.staticProfileImageUrlString]) { error in
                        if error != nil {
                            // show error
                            showError("user data blablabla")
                        }
                    }

CodePudding user response:

You are indeed adding a document with random ID (which is not same as user's UID). Instead use setData where you can specify the ID:

// Pass user ID from result of createUser
db.collection("users").document(result.user.uid).setData(data: ["username":self.usernameLabelRegister.text ,"vaccine":self.chosenVaccine,"nationality":self.chosenLocation, "uid":result!.user.uid,"profileImageUrl":RegisterViewController.staticProfileImageUrlString]) { err in
    if let err = err {
        print("Error writing document: \(err)")
    } else {
        print("Document successfully written!")
    }
}
  • Related