首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Kotlin在Cloud Firestore中设置数据

使用Kotlin在Cloud Firestore中设置数据
EN

Stack Overflow用户
提问于 2019-05-24 03:15:41
回答 1查看 972关注 0票数 0

我的firestore文档中有两个字段:一个是数字,另一个是注释,这是一个对象数组。

现在,我想向现有文档的comments数组中添加一个新注释。我知道我需要使用现有的注释创建一个临时列表,然后在该列表的末尾添加一个新的注释,最后使用Set方法将该临时列表放入firestore。我的问题是--如何做到这一点?我尝试使用下面的代码,但它只创建了一个对象列表,但我只想要一个对象列表。

代码语言:javascript
复制
FirebaseFirestore.getInstance()
        .collection("name of my collection")
        .document("name of particular document")
        .get().addOnSuccessListener {

            docs: DocumentSnapshot ->

            //this creates list of list of objects
                val data = listOf(docs.data!!["comments"])

                val tempOutput = data.toMutableList()
                tempOutput.add(newCommentInfo)

               //This prints list of objects
                Log.i(TAG, "${docs.data!!["comments"]}")

               //This prints list of list of objects which I don't want
                Log.i(TAG, "$tempOutput")

                FirebaseFirestore.getInstance()
                    .collection("name of my collection")
                    .document("name of particular document")
                    .set(newCommentInfo, SetOptions.merge())
EN

回答 1

Stack Overflow用户

发布于 2019-05-24 03:26:33

如果您只是尝试更新文档中的单个列表类型字段(从您的问题中并不完全清楚),使用update()方法并告诉它您正在修改哪个字段会更容易:

代码语言:javascript
复制
FirebaseFirestore.getInstance()
    .collection("name of my collection")
    .document("name of particular document")
    .update("comments", newCommentInfo)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56281656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档