[英]Firestore query - checking if username already exists
我需要有關 Firestore 查詢的幫助。 我有一個all_users
數據集合,包含每個用戶信息的用戶 ID 文檔。 firestore 數據庫圖像我想檢查用戶名是否已經存在。 我知道如何獲取()文檔並按照他們網頁上的演示進行比較,但是數據查詢呢?,這是我的代碼
更新小部件 - (如果 mUser 文本字段和當前用戶名不同)
private void saveProfileSettings(){
final String username = mUsername.getText().toString();
//Case 1: user did not change their username
if (!mUsers.getUsername().equals(username)){
checkingIfusernameExist(username);
}else {
}
}
檢查IfusernameExist 方法
private void checkingIfusernameExist(final String username){
Log.d(TAG, "checkingIfusernameExist: Checking if " + username + " Exists");
Query mQuery = mFirebaseFirestore.collection("all_users")
.orderBy(getString(R.string.fields_username))
.whereEqualTo("username", username);
mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (documentSnapshots != null){
Log.d(TAG, "onEvent: username does not exists");
Toast.makeText(getActivity(), "Username is available", Toast.LENGTH_SHORT).show();
}
for (DocumentSnapshot ds: documentSnapshots){
if (ds.exists()){
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH: " + ds.toObject(Users.class).getUsername());
Toast.makeText(getActivity(), "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
});
}
我沒有收到任何錯誤,結果也沒有。 我到處搜索,但沒有看到我的問題。 另外,我可以處理的查詢並不多。 任何更正將不勝感激,提前致謝。
更新:經過幾天的搜索,我實際上在下面得到的答案的幫助下想出了一個解決方案。 因此,由於 firestore 沒有操作邏輯,並且您希望在 .whereEqualTo 中不存在用戶名時進行更新,請使用任務查找包含任何有效負載。
對我有用的代碼
檢查IfUsernameExists 方法
private void checkingIfusernameExist(final String usernameToCompare){
//----------------------------------------------------------------
final Query mQuery = mFirebaseFirestore.collection("all_users").whereEqualTo("username", usernameToCompare);
mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Log.d(TAG, "checkingIfusernameExist: checking if username exists");
if (task.isSuccessful()){
for (DocumentSnapshot ds: task.getResult()){
String userNames = ds.getString("username");
if (userNames.equals(usernameToCompare)) {
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH -username already exists");
Toast.makeText(getActivity(), "username already exists", Toast.LENGTH_SHORT).show();
}
}
}
//checking if task contains any payload. if no, then update
if (task.getResult().size() == 0){
try{
Log.d(TAG, "onComplete: MATCH NOT FOUND - username is available");
Toast.makeText(getActivity(), "username changed", Toast.LENGTH_SHORT).show();
//Updating new username............
}catch (NullPointerException e){
Log.e(TAG, "NullPointerException: " + e.getMessage() );
}
}
}
});
以下查詢返回提供 usernameToCheck 的所有用戶。 如果用戶名是唯一的,那么你只會得到一個 documentSnapShot。
Query mQuery = mFirebaseFirestore.collection("all_users")
.whereEqualTo("username", "usernameToCheck");
mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentSnapshot ds: documentSnapshots){
if (ds!=null){
String userName = document.getString("username");
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH: " +userName );
Toast.makeText(getActivity(), "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
});
要解決此問題,請使用以下代碼行:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference allUsersRef = rootRef.collection("all_users");
Query userNameQuery = allUsersRef.whereEqualTo("username", "userNameToCompare");
userNameQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
if (document.exists()) {
String userName = document.getString("username");
Log.d(TAG, "username already exists");
} else {
Log.d(TAG, "username does not exists");
}
}
} else {
Log.d("TAG", "Error getting documents: ", task.getException());
}
}
});
其中userNameToCompare
是 String 類型,並且是要與之進行比較的用戶的用戶名。
由於我無法發表評論,因此我將其輸入為答案:
我想知道當 snapshotListener 運行時你的日志是什么樣的。
就像,當 spanshotListener 運行時,您的條件下的上述哪些日志正在打印在控制台上。
此外,您寫道您沒有得到任何結果,因此假設您希望看到 Toasts 但無法看到是否安全?
此外,在瀏覽您的代碼后,我根據此文檔發表了評論:
mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (documentSnapshots != null){
// you are checking here whether your querySnapshot is not null
// instead of checking whether it is null or empty.
// taking a wild guess here but could try to modify
// your if condition as follows -->
// if (documentSnapshots.isEmpty()) {
Log.d(TAG, "onEvent: username does not exists");
Toast.makeText(getActivity(), "Username is available", Toast.LENGTH_SHORT).show();
}
// below as well documentSnapshots is assumed to be not null,
// hence all the more reason I am taking the above wild guess
for (DocumentSnapshot ds: documentSnapshots){
if (ds.exists()){
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH: " + ds.toObject(Users.class).getUsername());
Toast.makeText(getActivity(), "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
});
我無法進一步調試您提供的數據。 但此鏈接可能對您有更多幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.