我想将用户数据存储为 Cloud Firestore 中管理员文档的子集合。

huangapple go评论78阅读模式
英文:

I want to store user data as a subcollection of the admin document in the Cloud Firestore

问题

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

数据库快照

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

声明:

FirebaseAuth auth;
String userId;
FirebaseFirestore firestore;

初始化并获取uid:

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

从文档传递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>() {
    //...
});
英文:

The Signup admin activity stores its details in the ADMIN collection document (admin name) but when I add professor details (through the admin portal) as a subcollection to the admin document using uid, it generates a new document with the name of uid.

Database snapshot

How can I store the details of the user/professor in a current admin document as a subcollection?
Please help me Thanks.

Declaration:

FirebaseAuth auth;
String userId;
FirebaseFirestore firestore; 

Initializing and Fetching uid:

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

Passing userId from Document:

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

答案1

得分: 1

问题在于 userId 与您在创建管理员文档时定义的 name 不对应。因此,当您使用此文档引用:DocumentReference documentReference = firestore.collection("Admin").document(userId).collection("Professor").document(professorname); 由于 userId 在您的数据库中不存在,所以会创建一个新文档。
请确保 userId 与数据库中的 name(id)对应,然后应该就能按预期工作了。

英文:

The problem is that userId does not correspond to the name you defined when creating the admin document. So when you use this document reference: DocumentReference documentReference = firestore.collection(&quot;Admin&quot;).document(userId).collection(&quot;Professor&quot;).document(professorname); a new document is created because userId does not exist in your database.
Make sure that userId corresponds to the name (id in the database) and it should then work as expected.

huangapple
  • 本文由 发表于 2020年10月21日 21:18:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64464405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定