Firebase数据更新

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

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(&quot;users&quot;);
reference.child(document.getId()).child(&quot;Role&quot;).setValue(&quot;3&quot;);

document.getId() shows the correct user id but nothing changes in the database

Database screen

答案1

得分: 0

好的,以下是翻译好的部分:

好的,这是一个愚蠢的错误。我正在使用Cloud Firestore,并尝试切换到
实时数据库
&lt;br&gt;&lt;br&gt;
正确的

    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(&quot;users&quot;).document(document.getId());
Map&lt;String,Object&gt; map = new HashMap&lt;&gt;();
map.put(&quot;Role&quot;,&quot;3&quot;);

 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(&quot;users&quot;);
DocumentReference uidRef = usersRef.document(uid);
uidRef.update(&quot;Role&quot;, &quot;3&quot;).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;Void&gt; task) {
        if (task.isSuccessful()) {
            Log.d(&quot;TAG&quot;, &quot;The document is updated successfully!&quot;);
        }
    }
});

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(&quot;Role&quot;).update(&quot;3&quot;);

than "setValue":

 reference.child(document.getId()).child(&quot;Role&quot;).setValue(&quot;3&quot;);

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

发表评论

匿名网友

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

确定