创建新用户后,此代码会覆盖数据库。这是我放入的代码:
func handleRegister() {
guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text
else {
print("Form is not Valid")
return
}
Auth.auth().createUser(withEmail: email, password: password, completion: {(user: User?, error) in
if error != nil {
print(Error.self)
return
}
guard (user?.uid) != nil else { return }
})
let ref = Database.database().reference(fromURL: "//Firebase link")
let usersReference = ref.child("Users").child("uid")
let values = ["name": name, "email": email]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err.self)
return
}
print("Successfully saved user in Firebase DB")
})
}
我觉得我错过了一些非常简单的事情。任何帮助,将不胜感激。
答案 0 :(得分:2)
编辑: 在你的
之后guard (user?.uid) != nil else {
return
}
将剩下的代码放在它下面:
let ref = Database.database()... //your code
//put inside the completion block of create user
//the rest of your code up to the updateChildValues
并在用户
中将此uid用作您孩子的参数所以它会变成:
let usersReference = ref.child("Users").child(uid)
请注意, uid 未附加双引号
您必须将其放在createUser 完成块中,因为用户?.uid 的范围仅限于该块。
答案 1 :(得分:0)
每次运行处理程序数据时都会在代码中使用下面的代码,因为您提供静态“uid”使用下面的代码来添加具有当前用户uid的节点,该用户uid在firebase DB中创建帐户时分配给用户< / p>
//before viewDidLoad declare a global databaseRef as
let databaseRef = Database.Database().reference //this will be used as base reference In all handlers
var name : String = ""
var Uid : String = ""
var email : String = ""
var nameString : String = ""
func createUser(){
Auth.auth().createUser(withEmail: self.EmailTextField.text!, password: self.PasswordTextField.text!) { (user, error) in
if error == nil {
print("You have successfully signed up")
//Goes to the Setup page which lets the user take a photo for their profile picture and also chose a username
self.nameString = self.nameTextField.text!
self.AddUserDetails()
DispatchQueue.main.async(execute: {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Main")
self.present(vc!, animated: true, completion: nil)
})
} else {
//this block shows error if exist
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
//this is my activity indicator
self.spinnerView.isHidden = true
self.spinner.isHidden = true
self.spinner.stopAnimating()
}
}
}
func AddUserDetails(){
//I make use of user nsobject class to get all details
//you can also make use of some global strings
self.name = self.nameString //name string store current display name
//name string contains name which I am taking as input when a user want to sign up as a textField text
self.email = (Auth.auth().currentUser?.email)! //current email
self.Uid = (Auth.auth().currentUser?.uid)! //current uid
let user:[String : AnyObject] = ["email": self.email as AnyObject,"name":self.name as AnyObject,"uID":self.Uid as AnyObject]
databaseRef.child("users").child(self.Uid).setValue(user)
}
答案 2 :(得分:-1)
你没有调用你的代码在用户创建的回调中写入数据库。因此,它使用旧的uid创建用户引用,从而覆盖值。
Auth.auth().createUser(withEmail: email, password: password, completion: {(user: User?, error) in
if error != nil {
print(Error.self)
return
}
// after this you are sure user created and now move your calls here
//to write in firebase database.
guard (user?.uid) != nil else {
return
}
})