检查用户是否存在Firebase 3.0 + Swift [英] Check if user exist with firebase 3.0 + swift
问题描述
我有一个应用程序,用户使用Firebase身份验证后,会将数据存储在Firebase数据库中。在存储数据之前,我想检查用户给出的用户名是否已经存在于数据库中。所以,如果不存在,我可以给用户这个唯一的用户名(就像每个用户有一个唯一的用户名)。所以我有一个 textField
用户输入他的用户名,然后按下一步。然后,应用程序应该检查用户名是否存在,并告诉用户是否需要更改它。
所以我用来检查用户名是否存在的代码:
pre $ let $ database $ ref $ ).observeSingleEventOfType(.Value,withBlock:{(snapshot)in
$ b $ if snapshot.hasChild(self.usernameTextField.text!){
print(user exists )
} else {
print(user does not exist)
}
})
$ c $因此每次按下下一个按钮时,都会调用此代码。这样做的问题是,结果总是保持与第一次搜索相同(即使在textField值更改后)。
例如,如果我搜索何塞,何塞和我的数据库存在,所以打印用户存在。但是,当我将
textField
更改为不存在的名称时,它仍显示user exists。 我想我需要将 .Value
更改为 FIRDataEventType.Value
if(usernameTextField.text?.isEmpty == false){
let databaseRef = FIRDatabase.database()。如果使用snapshot.hasChild(self.usernameTextField),则参考()
databaseRef.child(Users)。observeSingleEventOfType(FIRDataEventType.Value,withBlock:{(snapshot)in
。 ){
print(true room exists)
} else {
print(false room does not exist)
}
})
I have a app that after the user use Firebase auth it store the data on the Firebase database. Before storing the data, I want to check if the username the user give already exist in the database. So if it not exist I could give the user this unique username(like every user have a unique username). So I have a textField
where the user enter his username, and then press Next. Then the app should check if the username exist or not, and tell the user if he need to change it.
So the code I used to check if the username exist:
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Users").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if snapshot.hasChild(self.usernameTextField.text!){
print("user exist")
}else{
print("user doesn't exist")
}
})
So every time the next button is pressed, this code is called. The problem with this is that the result always remain the same as the first search (even after the textField value change).
For example, if I search Jose, and Jose exist in my database so is going to print "user exist". But when I change the textField
to name that don't exist, it still show "user exist".
I figured out I need to change the .Value
to FIRDataEventType.Value
if (usernameTextField.text?.isEmpty == false){
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Users").observeSingleEventOfType(FIRDataEventType.Value, withBlock: { (snapshot) in
if snapshot.hasChild(self.usernameTextField.text!){
print("true rooms exist")
}else{
print("false room doesn't exist")
}
})
这篇关于检查用户是否存在Firebase 3.0 + Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!