且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查用户名在Firebase中是否唯一

更新时间:2021-08-14 07:29:10

您在查询中缺少比较项:

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.