我正在尝试构建一个注册视图,以验证我的 Firestore 数据库中是否存在用户名值。我有另一个验证我的文本字段的函数,但我似乎无法弄清楚为什么这个函数在我的条件语句之后仍然执行 segue 以验证用户名是否存在于数据库中。
任何建议和改进都将不胜感激,因为我是 Swift 新手。:)
func signUpUser () {
db.collection("Users").getDocuments { (querySnapshot, error) in
if let error = error {
print("Error connecting to database")
}
else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
if let username = data["username"] as? String{
let usernameImput = self.usernameTextField.text
if usernameImput == username {
print("Username not available")
}
else if usernameImput != username {
let userID = Auth.auth().currentUser!.uid
if let name = self.nameField.text, let email = self.emailTextField.text, let username = self.usernameTextField.text {
self.db.collection("Users").document("\(userID)").setData(["name":name,"email":email, "username":username, "accountBalance": 0]) { (error) in
if error != nil {
// MARK: ERROR POPUP
print("Error saving data")
}
else {
self.performSegue(withIdentifier: "signedUp", sender: self)
}
}
}
print("Sign Up Successfully.")
}
else {
print("Other problems")
}
}
}
}
}
}
}
非常感谢您的任何帮助或贡献。:D