Firebase 实时数据库 – 更新现有数值

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

Firebase Realtime Database - Updating Existing Value

问题

我是新手使用Firebase,需要一些帮助。

我想要将“Cart”值插入到“MCwD6C_-XCZNGWxFLhO”用户中,“Cart”具有“itemName”、“quantity”和“itemPrice”字段。

我尝试了很多方法,但仍然无法做到。

这是我的当前Firebase结构

{
   -User
        -MCwD6C_-XCZNGWxFLhO
           email:testing@gmail.com
           username:testing
}

我想要插入新的数据,使其变成这样

{
  -User
       -MCwD6C_-XCZNGWxFLhO
           email:testing@gmail.com
           username:testing
           -Cart
              itemName:a
              itemPrice:66
}
英文:

I'm new to Firebase and I need some help.

I want to insert "Cart" value inside "MCwD6C_-XCZNGWxFLhO" user, "Cart" has "itemName","quantity","itemPrice" fields.

I tried a lot of ways still can't do it.

this is my current firebase structure

{
   -User
        -MCwD6C_-XCZNGWxFLhO
           email:testing@gmail.com
           username:testing
}

I want to insert new data become like this

{
  -User
       -MCwD6C_-XCZNGWxFLhO
           email:testing@gmail.com
           username:testing
           -Cart
              itemName:a
              itemPrice:66
}

答案1

得分: 1

要更新数据,您需要获取key

获取key的方法如下:

String key;

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
        .child("User");

Query query = databaseReference.orderByChild("username").equalTo("testing");

query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
            key = snapShot.getKey(); //Key
        }

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

要添加数据,可以使用以下代码:

HashMap<String, Object> map = new HashMap<>();
map.put("itemName", "a");
map.put("itemPrice", "66");

databaseReference.child(key).child("Cart").updateChildren(map);
英文:

You need the key to update data,

To get key:

String key;

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
                .child(&quot;User&quot;);

Query query = databaseReference.orderByChild(&quot;username&quot;).equalTo(&quot;testing&quot;);

query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
                    key = snapShot.getKey(); //Key
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                
            }
        });

To add date

HashMap&lt;String, Object&gt; map = new HashMap&lt;&gt;();
map.put(&quot;itemName&quot;, &quot;a&quot;);
map.put(&quot;itemPrice&quot;, &quot;66&quot;);

databaseReference.child(key).child(&quot;Cart&quot;).updateChildren(map);

答案2

得分: 0

如果您已保存了密钥-MCwD6C_-XCZNGWxFLhO,您可以使用setValue(newValue)来更新该值。有关更多信息,请查阅参考文档。例如,假设您已执行了mDatabase.child("User").child(yourKey).setValue(yourObject);,其中yourObject没有Cart对象或Cart对象为空。当您的Object中已经准备好Cart对象并希望推送该值时,再次执行相同的操作,这次您的Firebase数据将会更新。

英文:

If you have the key -MCwD6C_-XCZNGWxFLhO saved, you can just update the value using setValue(newValue). Check this reference for more. For example, let's say you have done mDatabase.child(&quot;User&quot;).child(yourKey).setValue(yourObject);
where yourObject has no Cart object or the Cart object is null. When you have the Cart object ready in yourObject and you want to push the value, do the same thing again and this time your firebase data will be updated.

huangapple
  • 本文由 发表于 2020年8月10日 12:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63334268.html
匿名

发表评论

匿名网友

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

确定