首页 文章

检查Firebase中是否存在用户名[重复]

提问于
浏览
-1

这个问题在这里已有答案:

我有这样的数据库

users / UID1 /“Username”(用户1)

/UID2/"Username" (of user 2)
 /UID3/"Username" (of user 3)
 /UID4/"Username" (of user 4)

等等..

我想检查用户名是否存在,但我没有设法进入所有现有UID的循环 . 现在我试过了:

let databaseRef = Database.database() . reference()

databaseRef.child("users").child("uid").child("Username").observeSingleEvent(of: DataEventType.value, with: { (snapshot) in
            switch snapshot.value {
            case let value as Bool where value:
                // value was a Bool and equal to true
                print ("username found")
            default:
                // value was either null, false or could not be cast
                print ("username not found")
            }
        })

    }

我不知道放什么而不是孩子(“uid”)循环到我的数据库中的每个uid并检查用户名是否存在

谢谢你的帮助 !

2 回答

  • 0

    实现这一目标的最简单方法是注册用户,使用其唯一的UID创建用户并将所有数据保存在那里,就像您正在做的那样但也创建一个名为“usernames”的节点,该节点只保存注册的所有用户名使用密钥作为用户名,值为1,如下所示:

    Usernames {
     - username: 1
    }
    

    当用户注册然后输入用户名时,您可以检查它是否存在如下:

    let username = "username that user has typed in"
    
    let reference = Database.database().reference()
    reference.child("usernames").observeSingleEvent(of: .value, with: { (snapshot) in
    
        if snapshot.hasChild(username) {
    
            print("Already exists")
    
        } else {
    
            print("Doesn't exist")
    
        }
    
    }, withCancel: nil)
    

    EDIT:

    感谢@FrankvanPuffelen,这是一种更有效的方法 - 无需遍历每个用户名 .

    let reference = Database.database().reference()
    reference.child("usernames").child(username).observeSingleEvent(of: .value, with: { (snapshot) in
    
        if snapshot.exists() {
    
            print("Username already exists")
    
        } else {
    
            print("Username doesn't already exist")
    
        }
    
    }, withCancel: nil)
    
  • 1

    In Android

    A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.

    DataSnapshot 有exists()方法检查记录是否存在 . 基于此文件https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html#exists()

    Like :

    reciverDatabase.getRef().addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.exists()) {
                                    reciverDatabase.child(Constants.CONVERSATION_UNREAD_COUNT).setValue(dataSnapshot.getValue(Long.class) + 1);
                                }
                            }
    
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                Crashlytics.logException(databaseError.toException());
                                sendMessageListener.onSendMessageFailure(databaseError.getMessage());
                            }
                        });
    

    In iOS :

    你可以检查它:

    For objective C : exists

    Desc : 如果DataSnapshot包含非空值,则返回YES .

    您可以从本文档中了解更多信息https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDataSnapshot#/c:objc(cs)FIRDataSnapshot(im)exists

    For Swift :

    exists()

    func exists() - > Bool

    https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DataSnapshot#/c:objc(cs)FIRDataSnapshot(im)exists

相关问题