【发布时间】:2020-07-21 15:40:02
【问题描述】:
我正在尝试调用 Firebase https 可调用函数,但我在 Android Studio 中收到错误消息“com.google.firebase.functions.FirebaseFunctionsException:响应不是有效的 JSON 对象”
这是我的代码
index.ts 文件
import * as functions from 'firebase-functions'
const admin = require('firebase-admin')
admin.initializeApp()
export { newUserSignUp } from './userCreated'
export { userDeleted } from './userDeleted'
//this is the function that the client is unable to call
exports.sendFeedback = functions.region('asia-east2').https.onCall((data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError(
'unauthenticated',
'only authenticated users can add requests'
)
}
if (data.text.length > 30) {
throw new functions.https.HttpsError(
'invalid-argument',
'request must be no more than 30 characters long'
)
}
return admin.firestore().collection('Feedback').add({
Feedback : data.text,
uid: context.auth.uid
})
})
这是我在 Android Studio 中的 .kt Activity 文件中的代码
private fun sendFeedbackViaCloudFunction() {
// Create the arguments to the callable function.
val data = hashMapOf(
"text" to write_feedback_edit_text.toString(),
"uid" to FirebaseAuth.getInstance().currentUser!!.uid
)
functions = FirebaseFunctions.getInstance()
Timber.i("Calling the cloud function")
functions
.getHttpsCallable("sendFeedback")
.call(data)
.addOnFailureListener {
//This is the line thats printing the error log statement
Timber.i("Failed to invoke the sendFeedback function: $it")
}
.continueWith { task ->
val result = task.result?.data as String
result
}
}
Android Studio 抛出的错误语句:com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object。
【问题讨论】:
-
你不需要显示转译的代码。我编辑了它 - 它没有添加任何有用的信息。
标签: android typescript firebase kotlin google-cloud-functions