I'll explain myself better with some examples now.
Basically I have a working login and registration process and whenever one user registers, he gets added to the DB on Firebase. Now, when he registers I get:
- Name
- Surname
and they all are added to the user information.
My Database has this structure:
Now, my task is to get the "nome" value (name in italian...) to print out , but how in the hell do I do this? The placeholder has to be substituted with the data from the DB and if the user changes it and presses the button it should modify the entry in the DB.
I read all the documentation and feel very stupid having not yet solved this problem.
Please help me!
This is all the code I have right now:
let db = Firestore.firestore()
var userID = Auth.auth().currentUser?.uid
userID = String(userID!)
let utenti = db.collection("utenti").document()
let nomeUtente = utenti.getDocument("nome")
Code of the button I use to register my users to the DB:
{
// Verifico nome e cognome
let erroreNome_Cognome = validazioneCampi()
if erroreNome_Cognome != nil {
// In caso non fossero compilati i campi do errore.
mostraErrore(erroreNome_Cognome!)
} else if let email = emailTextField.text, let password = passwordTextField.text { // Uso binding opzionale per evitare errori con i valori
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let e = error { // Uso di nuovo optional binding per errore
self.labelErrore.text = e.localizedDescription
} else {
// Pulisco i dati per il DB - Uso .trimmingCharacters per rimuovere, usando whitespacesAndNewlines, spazi e punti a capo
let nome = self.nomeTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let cognome = self.cognomeTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
// Quello che ho appena fatto, è possibile anche per la verifica dei dati svolta prima con erroreNome_Cognome. Infatti, senza di esso non potrei spacchettare il
// dato con "!".
// Qui creo l'utente nel database
let db = Firestore.firestore()
db.collection("utenti").addDocument(data: ["nome":nome, "cognome":cognome, "email": email, "uid": authResult!.user.uid]) { (erroreNome_Cognome) in
if erroreNome_Cognome != nil {
// Mostra errore
self.mostraErrore("I dati utenti non sono validi per il database")
}
}
// Naviga alla pagina successiva qui
self.performSegue(withIdentifier: "registratiAWelcome", sender:self)
}
}
}
}
CodePudding user response:
Ok, I can see that you already have your Firebase into your project.
Let's review your code:
When you are doing:
db.collection("utenti").document()
you are telling Firestore that you want to create a new document, and to create a reference to the future document. So nothing to do with this, if you want more information about this method: https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document
then when you are doing
utenti.getDocument("nome")
nome is a field of your document, it's not a document, and this document "nome" doesn't exists. If you want to query a specific document, you should do for example:
let db = Firestore.firestore()
let docRef = db.collection("utenti").document("cwmG6g0xgVe6txYSfKqm")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
}
More info about this here: https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document
Now with the current structure that you have on your database, what you would like to do base on you user uid is a query to all the documents, then this documents will have the fields like "nome"
let db = Firestore.firestore()
let userID = Auth.auth().currentUser!.uid
db.collection("utenti").whereField("uid", isEqualTo: userID).getDocuments { querySnapshot, error in
if let error = error {
print(error)
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
you have more information https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection and https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection
Hope this helps, I know you will have more questions, but play debugging the information and trying around to get a grip of Firestore SDK