Help placing a collection

Hello CodeWithChris I have attached my code and my collections that I wanted to add in my project. But I’m not sure where I’m meant to add it. Could you please tell me. Thanks in advance!

Hi Nakul,

When you post images of your code, use the built in screenshot app rather than take a photograph. Photographs of code are very hard to view.

With the Mac ScreenShot App, you can capture the entire window, the entire screen or a section of the screen.

If you just want a small section, press Shift + Command + 4 which will change your mouse pointer into a set of crosshairs. Click and drag diagonally across the screen to capture the section you want. When you let go of the mouse button, the image will be saved to your Desktop and will be named something like “Screen Shot 2020-04-24 at 1.22.57 pm”.

Other options are available by pressing Shift + Command + 5 and an options bar will appear near the bottom of the screen.

With regard to your question, have you been following one of the Firebase tutorials that Chris has published?

Yes I have been following the the fire base authentication tutorial. He doesn’t say to add other ones.

Is that this one?

Yes this is the one​:+1:t4::+1:t4::+1:t4::+1:t4::+1:t4::+1:t4::+1:t4::+1:t4:

OK a couple of points.

Don’t save the users password to the database as a document. The Firebase authentication process takes care of passwords so you don’t have to.

If you intend to save details about a user then you can consolidate that in a single document with mutilple fields inside the collection “users”. What you have created is a root collection for each data type rather than create a document of user details for each user inside the collection named users. Does that make sense?

Here’s a snapshot of my data in cloud Firestore page (I have edited the fields in Cloud Firestore to add bogus address, and city details so that when the program transitions to the HomeViewController I have a TableView that displays the data stored).

So what you would do is that as each user signs up you would create a document with the relevant fields like the following code I set up as a test. When you tap sign up, it saves the details.

    @IBAction func signUpTapped(_ sender: Any) {
        //  Validate the fields
        let error = validateFields()
        if error != nil {
            //  There is something wrong so show error message
        } else {
            //  Create the user
            Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
                if error != nil {
                    //  There was an error createing the user
                    self.showError("Error creating user.")
                } else {
                    //  User was successfully created, now store first name and last name.
                    let db = Firestore.firestore()
                    db.collection("users").addDocument(data: ["name" : self.firstName + " " + self.lastName, "address": "", "age": 50, "city": "", "postcode": 123456]) { (error) in
                        if error != nil {
                            self.showError("Error saving user data.")
                    //  User saved successfully
            //  Transition to the Home screen

I haven’t set up the signup form to request the details related to the fields I saved but just did it like this to prove that I understood what I needed to do

Thank you very much!!!