Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Firebase Functions ERROR: NSCocoaErrorDomain

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)
  }
}
like image 640
Lunke Avatar asked Jul 05 '18 07:07

Lunke


Video Answer


2 Answers

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")
like image 86
SteffenK Avatar answered Nov 15 '22 09:11

SteffenK


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.

like image 34
Simon Bengtsson Avatar answered Nov 15 '22 09:11

Simon Bengtsson