android - Firebase 函数抛出错误 com.google.firebase.functions.FirebaseFunctionsException : Response is not valid JSON object

标签 android typescript firebase kotlin google-cloud-functions

我正在尝试调用 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:响应不是有效的 JSON 对象。

最佳答案

好的,我解决了这个问题,所以基本上我反复收到“响应不是有效的 JSON 对象错误”,即使我 promise 在我的 index.ts 文件中返回一个有效的 JSON。

显然,如果客户端正在调用一个函数,那么客户端还需要指定 server region如果默认的 us-central1 不是首选服务器位置。所以我在服务器端指定了“asia-east2”作为我的偏好,但在客户端没有。

当我在客户端添加以下行后,现在可以完美运行了

functions = FirebaseFunctions.getInstance("asia-east2")

关于android - Firebase 函数抛出错误 com.google.firebase.functions.FirebaseFunctionsException : Response is not valid JSON object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61114038/

相关文章:

java - 尽管没有在任何地方清除,但构造函数中的 ArrayList 分配的数据正在丢失其数据

javascript - 在 Angular 8 中设置动态路由

javascript - 无法调用 jQuery

reactjs - 获取对组件类实例的引用

javascript - 在 FireStore 中过滤集合组查询

android - 通过 Firebase 中的 InstallReferrerReceiver 跟踪 Android 应用安装

android - 应用程序在构建时看不到模块类

android - 如何折叠/分组博览会通知

java - 无法从不同包中的 Activity 启动 Android 服务

swift - 转到详细的 TableView Controller