gpt4 book ai didi

java - 在 firebase 的节点内添加列表

转载 作者:行者123 更新时间:2023-12-02 08:49:02 25 4
gpt4 key购买 nike

我最近在将我想要的 json 结构添加到 firebase 数据库中时遇到了问题。我想向我的数据库添加一个额外的属性,如图 enter image description here 所示

我尝试了orderstReference.child(pid).child("quantity").setValue(orderId);
但每次执行时该值都会被覆盖,我希望它们像在列表中一样添加。

我该如何添加这个?有没有任何有用的链接来学习这些我找不到我想要的东西?

最佳答案

如果您已经有一个包含数据的节点,并且想要添加一个额外的子节点,那么使用 setValue() 将覆盖整个节点。在这种情况下,您需要使用 updateChildren():

private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

mDatabase.updateChildren(childUpdates);
}

检查此链接:

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

关于java - 在 firebase 的节点内添加列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60904206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com