尝试获取我的收藏集中的现有文档,但是它返回DocumentSnapshot为null。有任何线索吗?

时间:2019-04-01 12:40:11

标签: java android firebase google-cloud-firestore

我正在尝试从我的“用户”集合中获取文档,但是它将其返回为null。请看看我的收藏和文件 My collection

以下是我的代码

private void gettingCurrentUser(){
        String currentUserEmail = firebaseAuth.getCurrentUser().getEmail();
        DocumentReference docRef = firebaseFirestore.collection("users").document(currentUserEmail);
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if(task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if(document.exists()) {

                        test = document.getString("FirstName");

                    }
                    else {
                       Log.e(TAG, "Error message", task.getException());
                    }
                }
            }
        });
    }

我也尝试过一个字段一个字段,但是没有用

 private void gettingCurrentUser(){
    String currentUserEmail = firebaseAuth.getCurrentUser().getEmail();
    DocumentReference docRef = firebaseFirestore.collection("users").document(currentUserEmail);
    docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {

            String firstName = documentSnapshot.getData().get("FirstName").toString();
            String lastName = documentSnapshot.getData().get("LastName").toString();
            String emailAddress = documentSnapshot.getData().get("EmailAddress").toString();
            String dob = documentSnapshot.getData().get("DOB").toString();
            String address1 = documentSnapshot.getData().get("Address1").toString();
            String address2 = documentSnapshot.getData().get("Address2").toString();
            String city = documentSnapshot.getData().get("City").toString();
            String country = documentSnapshot.getData().get("Country").toString();
            String phonenumber = documentSnapshot.getData().get("Phonenumber").toString();

            User user = new User(firstName, emailAddress, lastName, phonenumber, address1, address2, city, country,dob);

            adminUser = user;

        }
    });
}

我认为这与对集合的异步调用有关,但是我不确定。正如您在我的log中所看到的,它确实返回了文档,但是由于某些原因,在为其分配“ adminUser”时始终为null。如果您能帮助我,也给我一些有关异步调用的指导,我将不胜感激。提前非常感谢您!


以下是当我尝试从集合中获取文档时指向我的日志的链接

https://wetransfer.com/downloads/2a134aa1396050585b18fa756fd7f48320190401143731/20844c


以下是我的代码,用于在我的一项活动中获取用户的名字,并且效果很好

 private void gettingData() {
   FirebaseUser user = firebaseAuth.getCurrentUser();
   String userEmail = user.getEmail();
   final DocumentReference docRef = firebaseFirestore.collection("users").document(userEmail);
   final String docPath = docRef.getPath();
   docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
       @Override
       public void onComplete(@NonNull Task<DocumentSnapshot> task) {
           if (task.isSuccessful()) {
               DocumentSnapshot document = task.getResult();
               if(document.exists()) {
                   Log.d(TAG, "Document Snapshot data: " + document.getData());
                   firstName = document.getData(). get("FirstName").toString();
                   textViewFirstName.setText("Welcome " + firstName);
               } else {
                   Log.d(TAG, "No such document");
               //    Toast.makeText(Home.this, "Failed Registration:2 "+task.getException(), Toast.LENGTH_SHORT).show();
               }
           } else {
               Log.d(TAG, "get failed with ", task.getException());
               //Toast.makeText(Home.this, "Failed Registration:3 "+task.getException(), Toast.LENGTH_SHORT).show();
           }
       }
   });
}

0 个答案:

没有答案