【问题标题】:Swift Firebase Functions ERROR: NSCocoaErrorDomainSwift Firebase 函数错误:NSCocoaErrorDomain
【发布时间】:2018-12-13 15:01:28
【问题描述】:

当我尝试从 Swift 运行 Firebase 函数时,我收到一条错误消息:

错误域 = NSCocoaErrorDomain 代码 = 3840 “JSON 文本没有以数组或对象和允许未设置片段的选项开头。” UserInfo={NSDebugDescription=JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。}

这是 Firebase 函数:

exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!")
})

这是我的 Swift 代码:

Functions.functions().httpsCallable("helloWorld").call { (result, error) in
  if error == nil {
    print("Success: \(result?.data)")
  } else {
    print(error.debugDescription)
  }
}

【问题讨论】:

  • 错误信息 "JSON text did not start..." 意思是它所说的。从受影响的数据中创建一个字符串,打印并检查出来。
  • 虽然“结果”变量为 nil...
  • 可能要发送的数据必须具有特定的(JSON)格式。
  • 可调用云函数和请求云函数是有区别的。请参阅this question,然后参考Call Functions via HTTP Requests 了解一些详细信息,因为我认为您想要 onCall 而不是 onRequest,那么也许不是,但无论哪种方式都是很好的信息。
  • @Lunke 对此有何更新?我遇到了同样的问题。

标签: swift function firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

如果您没有在客户端指定正确的区域,您可能会看到此错误。例如:

lazy var functions = Functions.functions(region: "europe-west1")

【讨论】:

  • 这太有帮助了!误导性错误消息的简单解决方案!
【解决方案2】:

response.send("Hello from Firebase!")

您不是在此处返回 JSON TEXT,而是在返回一个字符串。

你需要返回类似的东西

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

【讨论】:

    【解决方案3】:

    我的问题是我使用了http://localhost:5000 而不是正确的http://localhost:5001。模拟器默认模拟托管网站5000端口和5001端口功能。

    我通过评估FIRFunctions.m#261 中的回复意识到了这一点。它是托管网站的 html,显然无法解析为 json。

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 2021-11-18
      • 1970-01-01
      • 2017-06-16
      • 2018-08-18
      • 2020-05-06
      • 1970-01-01
      • 2020-12-06
      • 2018-08-01
      相关资源
      最近更新 更多