【问题标题】:Firebase Function throws an error com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON objectFirebase 函数抛出错误 com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object
【发布时间】: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


【解决方案1】:

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

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

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

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

【讨论】:

  • 就我而言,这与本地构建未更新以创建具有更新内容的 index.js 一起是原因。
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 2011-04-26
  • 2012-09-15
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
相关资源
最近更新 更多