Anu*_*nth 2 android kotlin firebase typescript google-cloud-functions
我正在尝试调用 Firebase https 可调用函数,但在 Android Studio 中出现错误,提示“com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object”
这是我的代码
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
})
})
Run Code Online (Sandbox Code Playgroud)
这是我在 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
}
}
Run Code Online (Sandbox Code Playgroud)
Android Studio 抛出的错误语句:com.google.firebase.functions.FirebaseFunctionsException:响应不是有效的 JSON 对象。
好的,我解决了这个问题,所以基本上我反复收到“响应不是有效的 JSON 对象错误”,即使我在 index.ts 文件中设置了返回有效 JSON 的承诺。
显然,如果客户端正在调用函数,那么如果默认的 us-central1 不是首选服务器位置,则客户端还需要指定服务器区域。所以我在服务器端指定了“asia-east2”作为我的偏好,而不是在客户端。
一旦我在客户端添加了以下行,现在就可以完美运行
functions = FirebaseFunctions.getInstance("asia-east2")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
897 次 |
| 最近记录: |