Reputation: 408
I am trying to invoke a Firebase https callable function and I get an error in Android Studio saying "com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object"
Here is my code
index.ts file
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
})
})
Here is the code in my .kt Activity file in 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
}
}
Error statement threw by Android Studio: com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object.
Upvotes: 2
Views: 2225
Reputation: 408
Ok I resolved this, So basically I was getting the "response is not valid JSON object error" repeatedly even after I set a promise to return a valid JSON in my index.ts file.
So apparently if a client is calling a function then the client also needs to specify the server region if the default us-central1 is not the preferred server location. So I had specified the 'asia-east2' as my preference on the server-side but not at the client-side.
Soon as I added the below line at the client side, works perfectly now
functions = FirebaseFunctions.getInstance("asia-east2")
Upvotes: 9