【发布时间】:2021-07-16 18:11:12
【问题描述】:
目前,我正在尝试在我的 android 应用程序中调用 https firebase 云函数。我得到的唯一问题是,当我尝试使用一些数据调用函数时,我得到了错误:
“对象不能用 JSON 编码”。
我的问题是:我该如何解决这个问题?我试图传递的对象是一个 kotlin 数据类。我必须先手动将数据转换为 json 对象(使用 gson)还是有更好的解决方案?
这是我目前的解决方案:(不工作,出现内部错误)
数据类
data class UserDeliveryAddress(
val company: String? = "",
val fullName: String = "",
val postcode: Int = 0,
val city: String = "",
val street: String = "",
val houseNumber: Int = 0,
val country: String = "",
)
用法
if (optionalAdress != null) {
val data = gson.toJson(userDeliveryNetworkEntityMapper.mapToEntity(optionalAdress))
dbFunctions.getHttpsCallable("authOnCreate")
.call(data) // just getting "internal" error
.await()
}
云功能
export async function doOnAuthCreate(data: any, context: functions.https.CallableContext) {
functions.logger.info("AUTH ON CREATE - Creating new user");
const dataObject = JSON.parse(data);
functions.logger.info(`Country is: ${dataObject.country}`);
functions.logger.info("AUTH ON CREATE - Sucessfully created user");
return 0;
}
索引.ts
import { doOnAuthCreate } from "./auth/onCreate.f"
export const authOnCreate = functions
.region(region)
.https
.onCall(doOnAuthCreate);
【问题讨论】:
-
您是否尝试将 JSON 转换为 HashMap?我认为可调用函数接受 HashMaps 作为数据。
-
@TarikHuber 是的,我做到了,仍然是内部错误。我认为本地模拟器有问题。我创建了一个新的哈希图,但仍然无法正常工作。必须找到另一个解决方案
-
您可以在调用可调用函数之前尝试 JSONfy 数据吗?
-
@FaridShumbar 没有改变任何东西
标签: android firebase kotlin google-cloud-functions