When I try to run a Firebase Function from Swift, I get an error saying:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
This is the Firebase Function:
exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!")
})
This is my Swift code:
Functions.functions().httpsCallable("helloWorld").call { (result, error) in
  if error == nil {
    print("Success: \(result?.data)")
  } else {
    print(error.debugDescription)
  }
}
                You may see this error if you haven't specified the correct region on the client side. For example:
lazy var functions = Functions.functions(region: "europe-west1")
                        My issue was that I used http://localhost:5000 instead of the correct http://localhost:5001. The emulator by default emulates the hosted website at port 5000 and the functions at port 5001.
I realized this by evaluating the response in FIRFunctions.m#261. It was the html of the hosted website which obviously cannot be parsed as json.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With