检查用户名是否已经存在:Swift,Firebase [英] Check if username already exist's : Swift, Firebase
问题描述
queryOrderedBychild
时,快照值总是可用的,但它会打印我的整个数据库,而不仅仅是我请求的数据 queryOrderby 。当我调用 queryEqualToValue
时,它总是返回null。我已经尝试了很多方法来弄清楚。
这是我的代码:
DataService.instance.UsersRef.queryOrdered(byChild:Nickname)。queryEqual(toValue:kai1004pro)。observe(.value,with:{(snapshot)in
if (snapshot.value是NSNull){
print(not found)
} else {
print(found)
print(snapshot.value)
}
$ b})
< (
$ b $ User User Profile(
$ = {
Birthday =Sep 20,1992;
Gender = Female;
Nickname = kai1004pro;
UserUID = g50X0FvEsSO4L457v7NzS3dAABl1;
emailAddress =poqksbs @ gmail。 con;
isFollow = 0;
isFriend = 0;
};
};
})
pre>
这是安全规则:
rules:{
.read:true,
.write:auth!= null,
Users:{
.read:true,
.write:auth!= null,
.indexOn:[Nickname, User Profile]
}
}
}
解决方案修改您的 JSON 树以包含单独的节点 active_usernames
,在添加每个用户的用户名时新用户,只要你的用户修改用户名就修改...
$ $ $ $ $ $ c $ myApp:{
users:{
uid1:{....},
uid2:{....},
uid3:{....},
uid4:{....},
}
active_usernames:{
uid1username:true,
uid2username:true,
uid3username:true,
uid4username:true
$ p要检查你的用户名是否已经存在: $> b
$ b
//检查用户名是否存在
FIRDatabase.database()。reference()。c如果usernameSnap.exists(){
)在
$ b中有observeSingleEvent(的:.value,其中:{(usernameSnap) //这个用户名已经存在
} else {
// Yippee!..这可以是我的用户名
}
} )
还要将您的安全规则更改为可读 (ONLY READABLE)
{rules:{
$ b $ em> 通过操作安全规则。 b active_usernames:{
.read:true,
.write:auth!= null
}
}
PS: 输入用户名
是输入用户名的文本字段。
建议: > didChangeEditing
textField的事件(为了更好的用户体验!!)
I'm trying to check if a username exists or not. When I call queryOrderedBychild
, snapshot value is always available but it will print down the whole database of mine, not just the data which I request queryOrderby
. When I call queryEqualToValue
, it always return null. I've tried many ways to figure it out.
This is my code:
DataService.instance.UsersRef.queryOrdered(byChild:"Nickname").queryEqual(toValue: "kai1004pro").observe(.value, with: { (snapshot) in
if (snapshot.value is NSNull) {
print("not found")
} else {
print("found")
print(snapshot.value)
}
})
This is my json tree:
Optional({
g50X0FvEsSO4L457v7NzS3dAABl1 = {
"User Profile" = {
Birthday = "Sep 20, 1992";
Gender = Female;
Nickname = kai1004pro;
UserUID = g50X0FvEsSO4L457v7NzS3dAABl1;
emailAddress = "poqksbs@gmail.con";
isFollow = 0;
isFriend = 0;
};
};
})
This is the security rules :
"rules": {
".read": true,
".write": "auth != null",
"Users": {
".read": true,
".write": "auth != null",
".indexOn": ["Nickname", "User Profile"]
}
}
}
解决方案 Modify your JSON tree to include a separate node active_usernames
, in that add username of every user whenever you create new user, modify whenever your user modify its username...
myApp:{
users:{
uid1:{....},
uid2:{....},
uid3:{....},
uid4:{....},
}
active_usernames :{
uid1username : "true",
uid2username : "true",
uid3username : "true",
uid4username : "true"
}
}
To check your if username already exists:-
//Checking username existence
FIRDatabase.database().reference().child("active_usernames").child(self.enteredUsername.text!).observeSingleEvent(of: .value, with: {(usernameSnap) in
if usernameSnap.exists(){
//This username already exists
}else{
//Yippee!.. This can be my username
}
})
Also change your security rules to be readable (ONLY READABLE) to all, by manipulating the security rules.
{rules :{
active_usernames : {
".read" : "true",
".write" : "auth != null"
}
}
}
PS:- enteredUsername
is the textField in which you enter your username.
Suggestion:- keep the checking code inside didChangeEditing
event of the textField(For better user experience.!)
这篇关于检查用户名是否已经存在:Swift,Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!