Lunke
Lunke

Reputation: 151

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)
  }
}

Upvotes: 7

Views: 1423

Answers (3)

Simon Bengtsson
Simon Bengtsson

Reputation: 8151

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.

Upvotes: 1

Muvimotv
Muvimotv

Reputation: 893

response.send("Hello from Firebase!")

You are not returning a JSON TEXT here but you are returning a string.

You need to return something like

response.send('{"message": "Hello from Firebase!"}')

Upvotes: 0

SteffenK
SteffenK

Reputation: 712

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")

Upvotes: 8

Related Questions