android - Firebase Function ทำให้เกิดข้อผิดพลาด com.google.firebase.functions.FirebaseFunctionsException: การตอบกลับไม่ใช่วัตถุ JSON ที่ถูกต้อง
ฉันกำลังพยายามเรียกใช้ฟังก์ชัน Firebase https callable และฉันได้รับข้อผิดพลาดใน 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
})
})
นี่คือโค้ดในไฟล์ Activity .kt ของฉันใน Android Studio
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 ที่ถูกต้อง
คำตอบ #1
โอเค ฉันแก้ปัญหานี้แล้ว โดยพื้นฐานแล้วฉันได้รับข้อผิดพลาด "response is not valid JSON object" ซ้ำ ๆ แม้ว่าฉันจะตั้งค่า promise ให้ส่งคืน JSON ที่ถูกต้องในไฟล์ index.ts ของฉันก็ตาม
ดังนั้นตามที่เห็นได้ชัด ถ้าลูกค้ากำลังเรียกใช้งานฟังก์ชัน ลูกค้าก็จำเป็นต้องระบุภูมิภาคของเซิร์ฟเวอร์ด้วยหากค่าเริ่มต้น us-central1 ไม่ใช่ตำแหน่งเซิร์ฟเวอร์ที่ต้องการ ดังนั้นฉันได้ระบุ 'asia-east2' เป็นความต้องการของฉันฝั่งเซิร์ฟเวอร์ แต่ไม่ได้ระบุที่ฝั่งลูกค้า
ทันทีที่ฉันเพิ่มบรรทัดด้านล่างที่ฝั่งไคลเอนต์ มันทำงานได้สมบูรณ์แบบแล้ว
functions = FirebaseFunctions.getInstance("asia-east2")
