我目前正在尝试使用create user function FIRAuth.auth()?.createUser
在此功能下,我尝试将数据插入数据库,如下所示:
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)
}
在我的视图控制器下,我可以使用以下命令创建对数据库的引用:
var ref = FIRDatabase.database().reference().child("users") //root database
我的网点都是正确的,但我收到了这个错误:
线程1:信号SIGABRT
有关我可能做错的任何建议?
编辑**以下是注册视图控制器中的所有代码
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)
}
}
}
}
编辑***这里是错误的屏幕截图
编辑****这是我的podfile的截图,当它更新时,我在终端中没有收到任何错误。
编辑*****控制台的屏幕截图。
答案 0 :(得分:0)
您必须像这样创建对数据库的引用
Var ref: FIRDatabaseReference!
然后在viewDidLoad中初始化数据库,就像这样
Ref = FIRDatabase.database().reference()
我在下面写了整个代码,原谅语法错误,因为我在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)
}
}
}
}