So, say user registration is done (this code is already correct) and the next window asks users to enter personal info (education etc), how do you code that this information goes to Firebase under the user's profile.

I have the registration page done and users created there already go to firebase.

This is what I have for the page after registration. No errors but obviously incomplete.

The below code is what I found, but it is only for entering data in to database. It doesn't work on 2 points:

  1. it it not dynamic data entered by user, it is simply the description of what the user is asked,
  2. it doesn't go under the user's profile in firebase.

func post() {

        let MainFunInterest = "MainFunInterest"
        let SomethingInterestingIhaveRead = "SomethingInterestingIhaveRead"
        let JobOrEducation = "JobOrEducation"
        let WhatIamConsideringBuying = "WhatIamConsideringBuying"

        let post : [String : AnyObject] = ["MainFunInterest" : MainFunInterest as AnyObject,
                                           "SomethingInterestingIhaveRead" : SomethingInterestingIhaveRead as AnyObject,
                                           "JobOrEducation" : JobOrEducation as AnyObject,
                                           "WhatIamConsideringBuying" : WhatIamConsideringBuying as AnyObject]

            let databaseRef = Database.database().reference()
            databaseRef.child("personal info").childByAutoId().setValue(post)

}

I need the user's entry to go under his profile in firebase.

2 Answers

0
Frank van Puffelen On Best Solutions

When you call databaseRef.childByAutoId() Firebase generates a new child node under databaseRef. So if you call that multiple times, you get multiple new child nodes, even when the calls are for the same user.

To store data for users, you'll want to (as Nathan answered) store that data under that user's unique UID. That way you can update it later if needed, and easily find the data for a user without having to query for it.

To store the data under the user's UID, do something like this:

let uid = Auth.auth().currentUser.uid
databaseRef.child("personal info").child(uid).setValue(post)

For more on this, see:

1
Community On

You can add a ‘users’ node through .child() and then set their personal information by their personal UID from .getUID

So the database could look like this:

Users - (Their personal UID) - Name - ‘Terry’ Email - ‘[email protected]’ Phone - ‘0978364727’