简体   繁体   English

如何访问从 Cloud Function(kotlin/typescript)返回给客户端的数组元素?

[英]How do I access the elements of the array returned to the client from a Cloud Function (kotlin/typescript)?

My app calls a cloud function called getGroupsInfo.我的应用程序调用名为 getGroupsInfo 的云 function。 It returns 3 values: result , message , and groupsInfo .它返回 3 个值: resultmessagegroupsInfo

In the Logcat, I printed out the 3 values and below is the result.在 Logcat 中,我打印了 3 个值,下面是结果。 I now need to use each element in the groupsInfo array, but I'm not sure how to access each one.我现在需要使用groupsInfo数组中的每个元素,但我不确定如何访问每个元素。 In the example below, I tried to access the first element but it didn't work.在下面的示例中,我尝试访问第一个元素,但没有成功。

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]

In my approach the easy way to access the elements is Iterating with for loop在我的方法中,访问元素的简单方法是使用 for 循环进行迭代


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何访问javascript函数返回的数组中的对象 - How do i access the objects inside the array returned by a javascript function 如何访问函数返回的数组中的数据? - How do I access data in an array that was returned by a function? 如何访问 Laravel 数组验证返回的元素? - How to access to the returned elements from Laravel array validation? 从Typescript中的函数返回数组 - get array returned from function in Typescript 如何将函数返回的数组复制到 JavaScript 中的另一个数组中? - How do I copy an array returned by a function into another array in JavaScript? 如何访问从 AJAX 请求返回的 JSON 数据? - How do I access JSON data returned from an AJAX request? JS中返回的多个值/如何从函数外部访问返回值数组? - Multiple value returned in JS/How to access an array of returned value from outside the function? 如何在 js/googlescript 中访问从客户端到服务器端的对象数组中传递的变量 - How do I access a variable passed in an object array from client side to server side in js/googlescript 打字稿:无法索引从函数返回的数组 - Typescript: Can't index an array that is being returned from a function 如何将异步 function 的返回值分配给变量 - how do I assign a returned value from an async function to a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM