如何检查用户名在Firebase中是否唯一 [英] How to check if usernames are unique in Firebase
问题描述
我有一个功能(尚未完成),该功能应检查用户名在Firebase数据库中是否已经存在/唯一.我已经在Google上搜索了如何实施此方法,并设法获得了以下信息.不幸的是,它不起作用,我什至没有任何console.logs.感谢您的帮助-我到底在做什么错?
I have a function (not finished yet) that should check if a username already exists/is unique in my Firebase database. I have searched Google on how to implement this and I have managed to get the below. Unfortunately, it doesn't work and I don't even get any console.logs. Any help is appreciated - what exactly am I doing wrong?
onChangeUsername = (event) => {
var oldError = {...this.state.error};
firebase.database().ref().child("Usernames").orderByValue().once('value', (snapshot) => {
var exists = (snapshot.val() !== null);
console.log(exists);
console.log("hello");
});
if(this.state.validation.username.unique===true)
{oldError.username = "This username is already taken"}
else if((event.nativeEvent.text.length<this.state.validation.username.min) || (event.nativeEvent.text.length>this.state.validation.username.max) )
{oldError.username = "Username should be between " + this.state.validation.username.min + " and " + this.state.validation.username.max + " characters" ;}
else oldError.username = "";
this.setState({ username: event.nativeEvent.text, error:oldError })
}
我的数据库结构如下:
Database:{ Usernames: {uid1: username1, uid2: username2}}
****问题更新**** 在弗兰克发表评论后,我将代码更改为以下代码.它仍然不起作用,但是我他发现了一些需要修复的错误:
****Question Update **** After Comments from Frank, I have changed my code to the following. It still doesn't work, but my he has spotted a couple of errors that needed fixing:
onChangeUsername = (event) => {
var oldError = {...this.state.error};
firebase.database().ref().child("Usernames").orderByKey().equalTo("Username1").once('value', (snapshot) => {
var exists = (snapshot.val() !== null);
console.log(exists);
console.log("hello");
});
if(this.state.validation.username.unique===true)
{oldError.username = "This username is already taken"}
else if((event.nativeEvent.text.length<this.state.validation.username.min) || (event.nativeEvent.text.length>this.state.validation.username.max) )
{oldError.username = "Username should be between " + this.state.validation.username.min + " and " + this.state.validation.username.max + " characters" ;}
else oldError.username = "";
this.setState({ username: event.nativeEvent.text, error:oldError })
}
和我的数据库如下:
Database:{ Usernames: {Username1: uid1, Username2: uid2}}
推荐答案
您在查询中缺少比较项:
You're missing a comparison in your query:
firebase.database().ref().child("Usernames").equalTo("Username1").orderByValue()
我建议顺便说一句反转数据结构.如果您希望用户名唯一,请使用这些名称作为父节点中的键:
I recommend btw inverting the data structure. If you want user names to be unique, use those as the keys in the parent node:
Database:{
Usernames: {
Username1: uidOfUserWithUsername1,
Username2: uidOfUserWithUsername2
}
}
这样,由于您不需要查询,因此您的查询将变得更简单,更快捷.此外,该值现在使您可以轻松查找有关使用此用户名的用户的更多信息.
This way your lookups will be simpler and fast, since you don't need a query. In addition, the value will now allow you to easily look up more information on the user who claimed this user name.
这篇关于如何检查用户名在Firebase中是否唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!