2017-02-18 59 views
0

當我選擇註冊..數據發送到Firebase身份驗證,但不存儲在數據庫中?誰能告訴我哪裏會出錯?如何在Swift中將註冊用戶寫入數據庫?

func handleRegister(){ 
     // Validation 
     guard let email = emailTextField.text, let password = PassTextField.text, let name = nameTextField.text 
     else{ 
      print("Please provide an Email and Password") 
      return 
     } 

     FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user: FIRUser?, error) in 

      if error != nil { 
       print(error!) 
       return 
      } 
      // Successfully authenticated user 
      // Saving Data to the Database 
      let ref = FIRDatabase.database().reference(fromURL: "https://chat-47e5b.firebaseio.com/") 
      let values = ["name": name, "email": email] 
      ref.updateChildValues(values, withCompletionBlock: { (err,ref) 
       in 

       if err != nil { 
        print(err!) 
        return 
       } 

       print("Saved user successfully into Firebase") 
      }) 
     }) 
} 

回答

2

你不這樣做是正確的,你應該首先得到的分貝參考:

self.ref = FIRDatabase.database().reference()

然後:

let values = ["name": name, "email": email] 
self.ref.child("users").child(user.uid).setValue(values) 

作爲一個側面說明,轉換這樣的:

if error != nil { 
    print(error!) 
    return 
} 

轉至:

guard let error = error else { 
    print(error) 
    return 
} 
相關問題