如何像图像中那样将集合添加到Firestore数据库中?

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

How can I add colection to Firestore Database as in image?

问题

I want to add colection to Firestore database like this image:

here

I want to add a symbol in ArrayList every time the function is called.

fun addsymbol() {
    val arrayList = arrayListOf<String>()
    arrayList.add(Coin.Coin_Info?.symbol!!)
    val postHashMap = hashMapOf<String, Any>()
    postHashMap.put("symbols", arrayList)

    Firestore.collection("Users").document("User1").set(postHashMap, SetOptions.merge())
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                System.out.println("Succes ")
            }
        }.addOnFailureListener {
            System.println(it.message.toString())
        }
}

Function working but it's not adding symbols to ArrayList, it's updating the ArrayList. How can I solve it?

英文:

I want to add colection to Firestore database like this image:

here

I want to add a symbol in Arraylist every time the function is called.

fun addsymbol(){
    val arrayList= arrayListOf&lt;String&gt;()
    arrayList.add(Coin.Coin_Info?.symbol!!)
    val postHashMap = hashMapOf&lt;String, Any&gt;()
    postHashMap.put(&quot;symbols&quot;,arrayList)

    Firestore.collection(&quot;Users&quot;).document(&quot;User1&quot;).set(postHashMap, SetOptions.merge())
        .addOnCompleteListener { task-&gt;
        if(task.isSuccessful){
            System.out.println(&quot;Succes  &quot;)
        }
    }.addOnFailureListener {
        System.out.println( it.message.toString())
    }

}

Function working but It's not adding symbols to arraylist , It's updating arraylist . How can I solve it?

答案1

得分: 1

不需要读取文档中的数组以添加新符号。您可以简单地将FieldValue#arrayUnion()作为参数传递给更新方法,如下所示:

Firestore.collection("Users").document("User1").update("symbols", FieldValue.arrayUnion(Coin.Coin_Info?.symbol!!))

我还建议附加一个完整的监听器,以查看是否有什么问题。

英文:

There is no need to read the array in the document in order to add a new symbol. You can simply pass FieldValue#arrayUnion() as an argument to the update method like this:

Firestore.collection(&quot;Users&quot;).document(&quot;User1&quot;).update(&quot;symbols&quot;, FieldValue.arrayUnion(Coin.Coin_Info?.symbol!!))

I also recommend attaching a complete listener, to see if something goes wrong.

答案2

得分: 0

问题在于每次调用addsymbol函数时,都会创建一个新的arrayList实例,然后向其中添加一个符号。然而,您没有检索来自Firestore数据库的现有符号数组,并将新符号附加到其中。要解决这个问题,您需要检索现有的符号数组,将新符号附加到其中,然后使用新数组更新Firestore数据库。

请尝试以下代码:

fun addsymbol(){
    Firestore.collection("User").document("Userid").get()
        .addOnSuccessListener { documentSnapshot ->
            if (documentSnapshot.exists()) {
                val symbols = documentSnapshot.get("symbols") as ArrayList<String>
                symbols.add(Coin.Coin_Info?.symbol!!)

                val postHashMap = hashMapOf<String, Any>()
                postHashMap.put("symbols", symbols)

                Firestore.collection("User").document("Userid").set(postHashMap, SetOptions.merge())
                    .addOnCompleteListener { task ->
                        if (task.isSuccessful) {
                            System.out.println("Success")
                        }
                    }.addOnFailureListener {
                        System.out.println(it.message.toString())
                    }
            } else {
                System.out.println("Document does not exist")
            }
        }
        .addOnFailureListener {
            System.out.println(it.message.toString())
        }
}
英文:

The issue is that every time you call the addsymbol function, you are creating a new instance of the arrayList and then adding a symbol to it. However, you are not retrieving the existing array of symbols from the Firestore database and appending the new symbol to it. To fix this, you need to retrieve the existing array of symbols, append the new symbol to it, and then update the Firestore database with the new array.

try the following:

fun addsymbol(){
    Firestore.collection(&quot;User&quot;).document(&quot;Userid&quot;).get()
        .addOnSuccessListener { documentSnapshot -&gt;
            if (documentSnapshot.exists()) {
                val symbols = documentSnapshot.get(&quot;symbols&quot;) as ArrayList&lt;String&gt;
                symbols.add(Coin.Coin_Info?.symbol!!)

                val postHashMap = hashMapOf&lt;String, Any&gt;()
                postHashMap.put(&quot;symbols&quot;, symbols)

                Firestore.collection(&quot;User&quot;).document(&quot;Userid&quot;).set(postHashMap, SetOptions.merge())
                    .addOnCompleteListener { task -&gt;
                        if (task.isSuccessful) {
                            System.out.println(&quot;Success&quot;)
                        }
                    }.addOnFailureListener {
                        System.out.println(it.message.toString())
                    }
            } else {
                System.out.println(&quot;Document does not exist&quot;)
            }
        }
        .addOnFailureListener {
            System.out.println(it.message.toString())
        }
}

huangapple
  • 本文由 发表于 2023年2月14日 04:52:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441075.html
匿名

发表评论

匿名网友

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

确定