Sarah Victoria Snyder Sarah Victoria Snyder - 1 month ago 34
Swift Question

Writing User Data to Firebase After Creating New User Account

I am currently trying to write the users information to firebase's database after using the create user function

FIRAuth.auth()?.createUser


Under this function I attempt to insert the data into the database like this:

FIRAuth.auth()?.createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in

if error == nil

{
let email = self.emailField.text
let firstName = self.firstnameField.text
let lastName = self.lastnameField.text


self.ref.child((user?.uid)!).setValue(["firstName": firstName,"lastName": lastName,"email": email])
self.performSegue(withIdentifier: "createaccountLandingPage", sender: sender)
}


It also may be important to mention that under my view controller I create the reference to the database using:

var ref = FIRDatabase.database().reference().child("users") //root database


My outlets are all correct, but I am getting this error:


Thread 1: signal SIGABRT


Any suggestions on what I may be doing incorrectly?

EDIT** Here is all my code in the signup view controller

import UIKit
import Firebase
import FirebaseDatabase

class CreateAccountViewController: UIViewController {

var ref = FIRDatabase.database().reference().child("users") //root database

@IBOutlet weak var firstnameField: UITextField!
@IBOutlet weak var lastnameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var confirmpasswordField: UITextField!
@IBOutlet weak var createAccountButton: UIButton!


override func viewDidLoad() {

super.viewDidLoad()

if (FIRAuth.auth()?.currentUser) != nil
{

}
else
{

}

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

@IBAction func createAccountAction(_ sender: AnyObject)

{

if self.confirmpasswordField.text != self.passwordField.text

{

}

else

{
FIRAuth.auth()?.createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in

if error == nil

{

let user = FIRAuth.auth()?.currentUser.uid
let email = self.emailField.text
let firstName = self.firstnameField.text
let lastName = self.lastnameField.text

self.ref.child("users").child("\(user)").setValue(["firstName": firstName,"lastName": lastName,"email": email])


self.performSegue(withIdentifier: "createaccountLandingPage", sender: sender)

}

else

{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)

let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)

self.present(alertController, animated: true, completion: nil)

}
}
}
}


EDIT*** here is a screenshot of the error

EDIT**** here is a screenshot of my podfile and when it is updated I recieve no errors in the terminal.

EDIT***** Screenshots of console.

1stScreenshotConsole
2ndScreenshotConsole

Answer

You have to create a reference to your database like this

Var ref: FIRDatabaseReference!

Then you initialize your database in your viewDidLoad like this

Ref = FIRDatabase.database().reference()

I wrote the whole code below, excuse the syntax errors because I am answering your question on an iPad.

import UIKit
import Firebase
import FirebaseDatabase

class CreateAccountViewController: UIViewController {

    var ref = FIRDatabaseReference! //create a reference for your database


@IBOutlet weak var firstnameField: UITextField!
@IBOutlet weak var lastnameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var confirmpasswordField: UITextField!
@IBOutlet weak var createAccountButton: UIButton!


override func viewDidLoad() {

    super.viewDidLoad()

//initialize your database
ref = FIRDatabase.database().reference()
      // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

}

@IBAction func createAccountAction(_ sender: AnyObject)

{

    if self.confirmpasswordField.text != self.passwordField.text

    {

    }

    else

    {
        FIRAuth.auth()?.createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in

            if error == nil

            {
                let user = FIRAuth.auth()?.currentUser.uid //get the users UID after registering
                let email = self.emailField.text
                let firstName = self.firstnameField.text
                let lastName = self.lastnameField.text

                self.ref.child("users").child("\(user!)").setValue(["firstName": "\(firstName!)", "lastName": "\(lastName!)", "email": "\(email!)"])

                self.performSegue(withIdentifier: "createaccountLandingPage", sender: sender)

            }

            else

            {
                let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)

                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)

                self.present(alertController, animated: true, completion: nil)

            }
        }
    }
}
Comments