检查用户名是否已存在:Swift,Firebase

时间:2022-09-25 23:46:44

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.

我正在尝试检查用户名是否存在。当我调用queryOrderedBychild时,快照值始终可用,但它将打印出我的整个数据库,而不仅仅是我请求queryOrderby的数据。当我调用queryEqualToValue时,它总是返回null。我已经尝试了很多方法来解决这个问题。

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:

这是我的json树:

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"]
    }
  }
}

1 个解决方案

#1


2  

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...

修改您的JSON树以包含一个单独的节点active_usernames,在每次创建新用户时添加每个用户的用户名,每当您的用户修改其用户名时进行修改...

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.

PS: - enteredUsername是您输入用户名的textField。

Suggestion:- keep the checking code inside didChangeEditing event of the textField(For better user experience.!)

建议: - 将检查代码保留在textField的didChangeEditing事件中(为了更好的用户体验。!)

#1


2  

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...

修改您的JSON树以包含一个单独的节点active_usernames,在每次创建新用户时添加每个用户的用户名,每当您的用户修改其用户名时进行修改...

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.

PS: - enteredUsername是您输入用户名的textField。

Suggestion:- keep the checking code inside didChangeEditing event of the textField(For better user experience.!)

建议: - 将检查代码保留在textField的didChangeEditing事件中(为了更好的用户体验。!)