Firebase task.result?.HTTP云函数调用中返回null的数据
我支持以下代码:Firebase task.result?.HTTP云函数调用中返回null的数据,firebase,kotlin,google-cloud-functions,Firebase,Kotlin,Google Cloud Functions,我支持以下代码: private fun search(g: String, l: GeoPoint): Task<String> { val data = hashMapOf( "uid" to user.uid, "first_name" to user.first_name, "age" to user.age, "gender" to user.gender, "bio" to user.
private fun search(g: String, l: GeoPoint): Task<String> {
val data = hashMapOf(
"uid" to user.uid,
"first_name" to user.first_name,
"age" to user.age,
"gender" to user.gender,
"bio" to user.bio,
"img1" to user.img1,
"img2" to user.img2,
"latitude" to l.latitude.toString(),
"longitude" to l.longitude.toString(),
"g" to g,
"fcmToken" to fcmToken,
"pronoun" to if (user.gender == "male") "him" else "her"
)
log("data: $data") // successfully logs all values as non-null
return functions
.getHttpsCallable("searchNearby")
.call(data)
.continueWith { task ->
log("result: ${task.result?.data}")
// This continuation runs on either success or failure, but if the task
// has failed then result will throw an Exception which will be
// propagated down.
val result = task.result?.data as String
result
}
.addOnSuccessListener { result ->
log("search(): success $result")
}
.addOnFailureListener { exception ->
log("search(): exception $exception") // exception kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String
log("search(): localMsg ${exception.localizedMessage}") // localMsg null cannot be cast to non-null type kotlin.String
log("search(): cause ${exception.cause}") // cause null
log("search(): msg ${exception.message}") // msg null cannot be cast to non-null type kotlin.String
}
}
文档已成功创建,并且
{result:“successfully created document”}
已成功发送回客户端,但它仍引发异常java.util.HashMap无法转换为java.lang.String
。知道问题出在哪里吗?调试起来也特别困难,因为它没有告诉您“我的云”函数中的错误是什么。您正在从可调用函数向客户端返回一个JavaScript对象:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const geofirestore = require('geofirestore');
const { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } = require('geofirestore');
const user = {};
exports.searchNearby = functions.https.onCall((data, context) => {
return admin.firestore().collection('blocks').add({
user1name: "1142",
user1bio: data.bio,
user1img1: data.img1,
user1img2: data.img2,
user1fcmToken: data.fcmToken,
user1pronoun: data.pronoun
}).then(result => {
console.log("Successfully created document");
return { result: "Successfully created document" };
}).catch(error => {
console.log("Error creating document " + error);
return { errorCreatingDocument: error };
});
});
return { result: "Successfully created document" };
该对象包含一个名为result
的属性。因为您的函数返回一个对象,所以您的客户机需要假定可调用的结果是一个映射。地图将有一个单独的条目,其键也被命名为result
。如果您只需在此处记录result
的值,这应该很容易验证:
val result = task.result?.data
与其将其转换为字符串,不如将其转换为Map
,然后从中提取名为result
的条目:
val data = task.result?.data as Map<String, *>
val result = data["result"]
由于这可能是由于Kotlin代码和云函数代码之间的交互,请将您的问题也编辑为。我们需要能够看到函数的输出。它可能没有生成您所期望的。啊,我认为错误应该出现在我的Kotlin代码中。我现在将尝试调试我的云函数-谢谢。我现在在edit@DougStevenson中添加了一个类似的问题-文档已成功创建并向客户端返回一个值,但我仍然收到一个错误。谢谢,我错过了那里的演员阵容-将其更改为
Map
修复了该问题
return "Successfully created document";