java - Firebase Cloud Firestore 引用返回 null

标签 java android firebase google-cloud-firestore

因此,我在 firestore 中创建了 firebase 用户个人资料,它将保存用户数据,例如姓名、电子邮件、电话号码和奖励积分。用户使用 Google 登录登录。

这就是我将数据写入 firestore 的方式。

 private void addNewUser() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        String uid = user.getUid();
        for (UserInfo profile : user.getProviderData()) {
            // Id of the provider (ex: google.com)
            String providerId = profile.getProviderId();

            // UID specific to the provider

            // Name, email address, and profile photo Url
            String name = profile.getDisplayName();
            String email = profile.getEmail();


            Map<String, Object> newUser = new HashMap<>();
            newUser.put("Nama", name);
            newUser.put("Email", email);




            // Add a new document with a generated ID
            db.collection("users").document(uid).set(newUser);
                        }


        }


    }

而且那个有效。

但是当我尝试使用文档引用检索数据时

    @Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        String curUser = user.getUid();
        DocumentReference documentReference = db.document(curUser);
        documentReference.addSnapshotListener(this.getActivity(), new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
                if (documentSnapshot.exists()) {
                    String userNama = documentSnapshot.getString(KEY_NAMA);
                    String userEmail = documentSnapshot.getString(KEY_EMAIL);

                    namaUser.setText(userNama);
                    emailUser.setText(userEmail);
                }
            }
        });




    } else {

        Intent intent = new Intent(Home.this.getActivity(), LoginActivity.class);
        startActivity(intent);


        }

    }

本例中的文档引用 curUser,返回 null。我不知道我做错了什么。

最佳答案

您需要在 DocumentReference 中包含集合名称 (users)

改变,

DocumentReference documentReference = db.document(curUser);

到,

DocumentReference documentReference = db.collection("users").document(curUser);

关于java - Firebase Cloud Firestore 引用返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56926940/

相关文章:

java - 在 Java 中传递带有异常的整数

android - 已 Root 的 Android 设备抛出 EACCES : permission denied exception when I try to get CPU's temperature

android - Google Analytics 跟踪中的 AnalyticsReceiver

android - Firebase 保存用户相关数据

firebase - Flutter Firebase的多个setData线程仅响应最后一个线程

java - 如何在 Java 中给定键字符串创建特定类的对象?

java - 从另一个类清除 JTextArea

java - 获取 java.lang.IllegalArgumentException : requirement failed while calling Sparks MLLIB StreamingKMeans from java application

java - 验证方法是否被调用 - 单元测试

firebase - react-native google api 限制