firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.69k stars 1.49k forks source link

Functions reports JSON fragment on empty response (i.e. from undeployed function) #9593

Open iKK001 opened 2 years ago

iKK001 commented 2 years ago

[REQUIRED] Step 1: Describe your environment

[REQUIRED] Step 2: Describe the problem

Trying to run a httpscallable Function I get the following error:

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set. around line 2, column 0." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set. around line 2, column 0., NSJSONSerializationErrorIndex=1}

On Web and Android, the FB-Function runs fine - only on iOS I get the above error message.

Here is my code:

First the more old-fashioned way

functions.httpsCallable("myFBFunctionName").call(["test": "hello"]) { result, error in
  if let error = error {
      print(error)    // see error message above
  }
  if let data = result?.data as? [String: Any], let text = data["invitationCode"] as? String {
    print(text)
  }
}

Or with the more modern SwiftUI way (same error, unfortunately as above):

Task {
    var functions = Functions.functions()    
    do {
        let result = try await functions.httpsCallable("myFBFunctionName").call(["test": "hello"])
        print("result: \(result)")
    } catch {
        print("error: \(error)")   // see error message above
    }
}

How do I call this function in iOS15.4, using SDK 8.14.0 ????

google-oss-bot commented 2 years ago

I found a few problems with this issue:

morganchen12 commented 2 years ago

Can you grab the JSON data and post it here? Feel free to redact any fields, the error is caused only by the structure and not the actual values.

iKK001 commented 2 years ago

Which json-data do you mean? According to the Firebase Documentation for Function calls in iOS, I have to provide the following:

functions.httpsCallable("addMessage").call(["text": inputField.text]) { result, error in 
// ... 
}

And this is exactly what I did in my code.

Therefore according to the doc, I have to bring it to the form of ["text": inputField.text] instead of json format.

You ask me for the json-data. Well, how do I get it ? Or is the documentation wrong ? I don't understand yet.

If still json format is necessary inside the function's call() method, then can you please post the correct documentation here and update the Firebase-Doc as well ? Thank you.

iKK001 commented 2 years ago

I am sorry - my mistake: The Firebase Function was NOT YET DEPLOYED. Therefore it is impossible to work.

Too bad the error message is very much misleading tough.

morganchen12 commented 2 years ago

Probably the right way to fix this is to wrap the JSON deserialization error in another error to provide more context (with the NSUnderlyingError key). The line is here: https://cs.opensource.google/firebase-sdk/firebase-ios-sdk/+/master:FirebaseFunctions/Sources/Functions.swift;l=346