英文:
Firebase data update
问题
我想要改变用户角色。<br>
我有一个包含用户ID的用户集合。每个用户都有包含邮件、姓名、角色等字段。
DatabaseReference reference;
reference = FirebaseDatabase.getInstance().getReference("users");
reference.child(document.getId()).child("Role").setValue("3");
document.getId() 显示了正确的用户ID,但数据库中没有发生任何变化。
英文:
I want to change the user role.<br>
I have a collection of users with the user id. Each user has fields with mail, name, role, etc.
DatabaseReference reference;
reference = FirebaseDatabase.getInstance().getReference("users");
reference.child(document.getId()).child("Role").setValue("3");
document.getId() shows the correct user id but nothing changes in the database
答案1
得分: 0
好的,以下是翻译好的部分:
好的,这是一个愚蠢的错误。我正在使用Cloud Firestore,并尝试切换到
实时数据库
<br><br>
正确的
DocumentReference docfer = db.collection("users").document(document.getId());
Map<String, Object> map = new HashMap<>();
map.put("Role", "3");
docfer.update(map)
如果您需要进一步的帮助,请随时告诉我。
英文:
Ok it was stupid mistake. I'm working on Cloud Firestore and I've tried to change to
Realtime Database
<br><br>
Correct
DocumentReference docfer=db.collection("users").document(document.getId());
Map<String,Object> map = new HashMap<>();
map.put("Role","3");
docfer.update(map)
答案2
得分: 0
你的截图显示的是 Firestore 数据库,并不是 Firebase 实时数据库。这两者都属于 Firebase 服务,但实际上是两种完全不同的产品。因此,你需要使用属于 Firestore SDK 的方法。要更改 "Role" 属性的值,请使用以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
DocumentReference uidRef = usersRef.document(uid);
uidRef.update("Role", "3").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "文档已成功更新!");
}
}
});
结果将会将 "Role" 属性从 "0" 更新为 "3"。
英文:
Your screenshot displays a Firestore database and not a Firebase Realtime Database. Both are apart of Firebase services but are actually two totally different products. So you need to use method that are apart of Firestore SDK. To change the value of the "Role" property, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
DocumentReference uidRef = usersRef.document(uid);
uidRef.update("Role", "3").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "The document is updated successfully!");
}
}
});
The result will be the update of the "Role" property from "0" to "3".
答案3
得分: 0
使用 "update":
reference.child(document.getId()).child("Role").update("3");
而不是 "setValue":
reference.child(document.getId()).child("Role").setValue("3");
英文:
Use "update" :
reference.child(document.getId()).child("Role").update("3");
than "setValue":
reference.child(document.getId()).child("Role").setValue("3");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论