[英]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 个值:
result
、 message
和groupsInfo
。
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.