@objc func registerButtonClicked()
{
///在配置Firebase后,将其称为
guard let email = emailTextField.text else {
alertBox(title: "Error", message: "All fields are mandatory")
return }
guard let password = passwordTextField.text else { return }
guard let name = nameTextField.text else { return }
Auth.auth().createUser(withEmail: email, password: password) { (data, error) in
if error != nil{
print(error.debugDescription)
print("Error occurred")
} else
{
print("Data -- >\(String(describing: data))")
self.saveDataInFirebase(name: name, password: password, email: email)
///此处数据保存在firebase的身份验证表中,因此下一步// //将其详细信息保存在json中的db中
}
}
}
func saveDataInFirebase(name: String, password: String, email: String)
{
let userData = ["name" : name, "email" : email]
print("name \(name) --- Email \(email)")
//打印要保存在数据库中的详细信息
let firebaseRef = Database.database().reference(fromURL: "https://chatdemo1-d3423.firebaseio.com/")
firebaseRef.updateChildValues(userData) { (error, dbRef) in
if error != nil{
print("------------------")
print(error.debugDescription)
print("----------------------")
}
else
{
print("Data Saved Successfully")
}
}
}
答案 0 :(得分:0)
问题中的代码可能不是您想要使用的代码,因为它每次运行时都会覆盖整个Firebase。
原因是您没有将数据写入Firbase的子节点,而是将数据写入主节点。这是写的:
your_firebase //<- this is where you're writing too each time
email: "some email"
name: "some name"
我的猜测是您想要这样构造
your_firebase
user_id
email: "some email"
name: "some name"
执行该操作的代码将是
var ref: DatabaseReference!
func viewDidLoad() {
self.ref = Database.database().reference()
.
.
.
}
func createUser() {
let email = "some email"
let password = "some password"
let name = "some name"
Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
if let x = error { //some fancy error checking
let err = x as NSError
switch err.code {
case AuthErrorCode.emailAlreadyInUse.rawValue:
print("email in use!")
default:
print("unknown error")
}
} else {
guard let user = authResult?.user else { return }
let uid = user.uid
let usersRef = self.ref.child("users")
let thisUserRef = usersRef.child(uid)
thisUserRef.child("email").setValue(email)
thisUserRef.child("name").setValue(name)
}
}
}
此代码假定Firebase规则允许用户权限写入用户节点