【问题标题】:I want to store user data as a subcollection of the admin document in the Cloud Firestore我想将用户数据存储为 Cloud Firestore 中管理文档的子集合
【发布时间】:2020-10-21 13:18:04
【问题描述】:

注册管理员活动将其详细信息存储在 ADMIN 集合文档(管理员名称)中,但是当我使用 uid 将教授详细信息(通过管理员门户)作为子集合添加到管理员文档时,它会生成一个名为uid。

Database snapshot

如何将用户/教授的详细信息作为子集合存储在当前管理文档中? 请帮助我,谢谢。

声明:

FirebaseAuth auth;
String userId;
FirebaseFirestore firestore; 

初始化和获取uid:

firestore = FirebaseFirestore.getInstance();
auth = FirebaseAuth.getInstance();
userId = auth.getCurrentUser().getUid();

从 Document 传递 userId:

 DocumentReference documentReference =  firestore.collection("Admin").document(userId).collection("Professor").document(professorname);
                                    Map<String, String> Professor = new HashMap<>();
                                    Professor.put("Name", professorname);
                                    Professor.put("Email", email);
                                    Professor.put("Phone", phone);
                                    Professor.put("Desgination",desig);
                                    Professor.put("Department",dept);
                                    documentReference.set(Professor).addOnSuccessListener(new OnSuccessListener<Void>()
                                    {...}

【问题讨论】:

  • 如果您遇到问题,最好在发布问题时创建MCVE。您为此问题发布了近 300 行代码。人们要解析和尝试在线调试的内容很多。请编辑您的问题并隔离问题,这样可以增加获得帮助的机会。

标签: java android firebase google-cloud-firestore


【解决方案1】:

问题是userId 与您在创建管理文档时定义的name 不对应。因此,当您使用此文档参考时:DocumentReference documentReference = firestore.collection("Admin").document(userId).collection("Professor").document(professorname); 会创建一个新文档,因为您的数据库中不存在 userId。 确保 userId 对应于 name(数据库中的 ID),然后它应该可以按预期工作。

【讨论】:

    猜你喜欢
    • 2018-04-23
    • 2021-09-28
    • 1970-01-01
    • 2019-03-26
    • 2018-11-19
    • 2018-03-22
    • 2018-05-10
    • 2018-03-17
    相关资源
    最近更新 更多