英文:
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.
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("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>()
{...}
答案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("Admin").document(userId).collection("Professor").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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论