作者:郑小斌-杭州 | 来源:互联网 | 2023-05-16 23:56
IhaveaappthataftertheuseruseFirebaseauthitstorethedataontheFirebasedatabase.Befor
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.
我有一个应用,在用户使用Firebase身份验证后,它会将数据存储在Firebase数据库中。在存储数据之前,我想检查用户提供的用户名是否已存在于数据库中。因此,如果它不存在,我可以给用户这个唯一的用户名(就像每个用户都有一个唯一的用户名)。所以我有一个textField,用户输入他的用户名,然后按Next。然后应用程序应检查用户名是否存在,并告诉用户是否需要更改用户名。
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".
因此,每次按下下一个按钮,都会调用此代码。这样的问题是结果始终与第一次搜索保持相同(即使在textField值更改之后)。例如,如果我搜索Jose,并且Jose存在于我的数据库中,那么将打印“user exists”。但是当我将textField更改为不存在的名称时,它仍然显示“用户存在”。
2 个解决方案