0

I am currently trying to structure and organize my Firestore Database in my app. Essentially, the first collection of my app will contain my "users" with documents labeled with the email of every user who signs up. I was successfully able to label each document in my "users" database by referencing the Authentication module of Firebase but cannot seem to append data under the individual document paths. I don't know if this is bad practice or not, but this application is for demonstration purposes.

This works successfully in creating an email references in the docs

let db = Firestore.firestore()
                       
db.collection("users").document("\(userEmail)").setData(["firstname":firstname, "lastname":lastname, "uid": result!.user.uid,"email address": email ]) { (error) in
       if error != nil {
           self.showError("Email has already been used")

This is where I have a problem and the all of the new information is being stored under the a FIRDocumentReference

let iceName1 = iceName1TextField.text!
    
let iceNumber1 = iceNumber1TextField.text!

let iceName2 = iceName2TextField.text!

let iceNumber2 = iceNumber2TextField.text!


let currentUserEmail = Auth.auth().currentUser!.email
//using this, we can recieve the users email. I don't really know how else to recieve user email so we will need to have someone do that.
let docData: [String : Any] = ["ICE-Name-1" : iceName1 , "ICE-Number-1" : iceNumber1 , "ICE-Name-2" : iceName2, "ICE-Number-2" : iceNumber2]

let userEmail = db.collection("users").document("\(String(describing: currentUserEmail))")

// apparently the document that I am describing above may not exist, therefore, swift is creating document references that are no existent. We need to fix this.


//store data into firebase
let db = Firestore.firestore()


db.collection("users").document("\(userEmail)").setData(docData)
//this appends following information to firebase database
saveLabel.alpha = 1
    
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I don't understand your question. What is happening (example?), and what do you expect? – j1mbl3s Jun 04 '21 at 23:48
  • Do you have any problems to save the user using a unique ID ? – Menaim Jun 05 '21 at 00:44
  • @j1mbl3s Firestore is creating a document labeled (for example) instead of following a document path (userEmail) and appending information in the text fields. – Developer Wes Jun 05 '21 at 00:45
  • @Menaim no I do not. Whenever a user is created, Firebase creates a Unique user ID and I can view each UID in the firebase authentication module. – Developer Wes Jun 05 '21 at 00:49
  • I will write an answer an hope it helps, give me moments – Menaim Jun 05 '21 at 00:50
  • You are passing `userEmail` as a string to the second `.doc()` call, which is itself a Firebase document reference and not the email. That is outputting the object as its reference value in the `,doc()` call. What were you expecting to happen with that? – j1mbl3s Jun 05 '21 at 00:59
  • @j1mbl3s sorry, I am new to Firebase. So the documents I am referencing in question are a collection of User Emails so passing the constant 'userEmail' through the '.doc()' property should reference the document, within the collection, with said user's particular email. Or at least I think. Is there another way to do this? – Developer Wes Jun 05 '21 at 01:08
  • You are right in your approach but wrong in your execution. You set `let userEmail = db.collection("users").document("\(String(describing: currentUserEmail))")` and then save a document with `db.collection("users").document("\(userEmail)").setData(docData)`. In the second call, you are converting a document reference from the first into a string, the output of which is iits class name and memory reference. – j1mbl3s Jun 05 '21 at 01:22
  • @j1mbl3s ahh I understand now. What may you suggest I do? So from what I am hearing. My should my first constant instead be ` let userEmail = db.collection("users").document("\ (currentUserEmail)")`. Or am I completely missing the point. – Developer Wes Jun 05 '21 at 01:44

1 Answers1

0

In the provided code, you are passing a FIRDocumentReference instance as a string to collection.document(). When you stringify an object instance without a description string, it becomes something like <ClassName 0xHexBasedMemoryAddress>.

Try something like this instead:

let userEmailDoc = db.collection("users").document("\(String(describing: currentUserEmail))")
userEmailDoc.setData(docData)
j1mbl3s
  • 994
  • 3
  • 16