【问题标题】:How do I access the elements of the array returned to the client from a Cloud Function (kotlin/typescript)?如何访问从云函数(kotlin/typescript)返回给客户端的数组元素?
【发布时间】:2021-05-10 01:21:16
【问题描述】:

我的应用调用名为 getGroupsInfo 的云函数。它返回 3 个值:resultmessagegroupsInfo

在 Logcat 中,我打印了 3 个值,下面是结果。我现在需要使用groupsInfo 数组中的每个元素,但我不确定如何访问每个元素。在下面的示例中,我尝试访问第一个元素,但没有成功。

fs.getGroupsInfo()
            .continueWith { task ->
                if (task.isSuccessful) { 
                    Log.e(tag, "Result1: ${task.result?.get("result")}") //case sensitive
                    Log.e(tag, "Result2: ${task.result?.get("message")}") //case sensitive
                    Log.e(tag, "GroupsInfo: ${task.result?.get("groupsInfo")}") //case sensitive
                    val result = task.result?.get("result")
                    val message = task.result?.get("message")
                    if (result == 1) {    
                        var groupsInfo: Array<Any?> = arrayOf(task.result?.get("groupsInfo"))
                        Log.e(tag, groupsInfo[0].toString())

                    } else {
                        Log.e(tag, "Loading failed.")
                        Toast.makeText(baseContext, "Could not load groups.", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    Log.e(tag, "Result3: ${task.result?.get("result")}") //case sensitive
                    Log.e(tag, "Result4: ${task.result?.get("message")}") //case sensitive
                    Toast.makeText(baseContext, "There was a system error while trying to load the groups.", Toast.LENGTH_SHORT).show()
                }
            }

2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: Result1: 1
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: Result2: 
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: GroupsInfo: [73Xwaz7vAOM39e1KVPu8, NOti0G0Me0VSte48I2O0]
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: Loading groups was successful!
2021-05-09 20:13:33.062 8512-8512/com.example.2 E/_ActivityGroups: [73Xwaz7vAOM39e1KVPu8, NOti0G0Me0VSte48I2O0]

【问题讨论】:

    标签: javascript java android typescript kotlin


    【解决方案1】:

    在我的方法中,访问元素的简单方法是 使用 for 循环进行迭代

    
    task.result?.get("groupInfo").toList().map { /**incase it's not string*/ it.toString() } .forEach {
          Log.d(tag, "$it")
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 2011-04-08
      • 1970-01-01
      • 2023-04-03
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多