我正在尝试验证用户输入的用户名是否已被占用。该代码目前检查电子邮件是否正在使用,并创建用户,如果他们不存在。我怎样才能使这个数据库读取,看看用户名是否已被使用?如果用户名已被占用,请检查Firebase数据库
FIRAuth.auth()?.createUser(withEmail: email!, password: password!) { (user, error) in
if error?._code == (FIRAuthErrorCode.errorCodeInvalidEmail.rawValue)
{
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.child("Users").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
if snapshot.hasChild(name!){
ref.child("users").child((user?.uid)!).setValue(["username": name])
print("true rooms exist")
} else {
self.displayMyAlertMessage(userMessage: "Username already in use. Please choose another.")
print("false room doesn't exist")
}
})
} else {
self.displayMyAlertMessage(userMessage: "Email already in use. Please see the login page.")
return
}
}
嗯,这不是“像写”你的数据库后端?你指的是'the'数据库,哪一个?你有任何代码写入永久存储? – Yohst
我正在使用firebase,因此数据库已经存在。代码存储的代码是ref.child的代码。 – AlexGarlock
这已被问过“几次”之前。请参阅[这些答案](http://stackoverflow.com/search?q=%5Bfirebase-database%5D%5Bios%5D+unique+username)或[来自加藤的原始答案](http://stackoverflow.com/问题/ 25294478/how-do-you-prevent-duplicate-user-properties-in-firebase)(对于JavaScript,但所有平台的逻辑都是相同的)。 –