2017-10-04 139 views
0

我想在Cloud Firestore中創建用戶數據庫,並將電子郵件作爲每個用戶的唯一ID。用戶應該在collection,每個用戶是document檢查用戶是否存在於Firestore

這是我的檢查,如果用戶在登錄活動存在:

FirebaseFirestore db = FirebaseFirestore.getInstance(); 
          final DocumentReference userRef = db.collection("users").document(email); 
          userRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { 
           @Override 
           public void onComplete(@NonNull Task<DocumentSnapshot> task) { 
            if (task.isSuccessful()) { 
             DocumentSnapshot document = task.getResult(); 

             Toast.makeText(LoginActivity.this, task.getResult().toString(), 
               Toast.LENGTH_SHORT).show(); 

             if (document != null) { 
              //The user exists... 
             } 


             else { 
              //The user doesn't exist... 
              } 

             } 
            } 
            else 
             connectionErrorActivity(); 

           } 
          }); 

的數據庫是空的,因爲你可以看到:

enter image description here

我的問題是, document從不爲空,即使數據庫爲空也很難。toast顯示字符串值document是:[email protected]

這是正確的方法嗎?我能做些什麼來解決它?

回答

5

這個Task的結果是DocumentSnapshot。底層文檔是否實際存在可通過exists方法獲得。

如果文檔確實存在,您可以致電getData查看其中的內容。