下面是允许用户将数据存储到 Firestore 的代码。问题是,所有登录应用程序的用户都可以查看上传的数据。我怎么能只允许当前登录的用户只存储他们帐户的数据?使用 Firebase 数据库,我使用以下代码(有效,但不适用于 Firestore)FirebaseUser 用户 = FirebaseAuth.getInstance().getCurrentUser(); String userId=user.getUid(); mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH).child(userId);上述代码行允许仅为登录用户存储和检索数据。因此,数据仅对他们而言是唯一的。我如何为 Firestore 复制它? fetch=findViewById(R.id.fetchDocument); firestore = FirebaseFirestore.getInstance(); upload =findViewById(R.id.uploadText); upload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { POJO pojo = new POJO(); pojo.setValue("Hello World"); //below code creates collection, creates a inner doc. and uploads the value (pojo) firestore.collection("RootCollection").document("MyDoc") .set(pojo).addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { //gets called if upload is successful Toast.makeText(ichild.this,"Upload is successful", Toast.LENGTH_SHORT).show(); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(ichild.this,"Upload is not successful" + e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } }); fetch.setOnClickListener(new View.OnClickListener() {
2 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
如何使用 Firestore 仅存储当前登录用户的数据
如果将 传递uid给该document()方法,则可以实现相同的目的。所以 Firebase 实时数据库中使用了以下几行代码:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH).child(userId);
在 Firestore 中等效于:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference uidRef = rootRef.collection("users").document(uid);
POJO pojo = new POJO();
uidRef.set(pojo);
添加回答
举报
0/150
提交
取消