Home > front end >  How to use FirebaseFunctions and log events or errors?
How to use FirebaseFunctions and log events or errors?

Time:10-10

Since there's only documentation for Node.js, it's unclear of how to use FirebaseFunctions Swift library. I will appreciate if someone can provide some basic examples.

CodePudding user response:

As @jnpdx pointed out in their comment, Firebase only support writing Callable Cloud Functions in Node.js.

What you can do though is call your Cloud Functions from Swift, as shown in the documentation here:

functions.httpsCallable("addMessage").call(["text": inputField.text]) { result, error in
  if let error = error as NSError? {
    if error.domain == FunctionsErrorDomain {
      let code = FunctionsErrorCode(rawValue: error.code)
      let message = error.localizedDescription
      let details = error.userInfo[FunctionsErrorDetailsKey]
    }
    // ...
  }
  if let data = result?.data as? [String: Any], let text = data["text"] as? String {
    self.resultField.text = text
  }
}

And to handle errors:

if let error = error as NSError? {
  if error.domain == FunctionsErrorDomain {
    let code = FunctionsErrorCode(rawValue: error.code)
    let message = error.localizedDescription
    let details = error.userInfo[FunctionsErrorDetailsKey]
  }
  // ...
}

So it's usually a two-step process:

  1. Write your Cloud Functions in Node.js.
  2. Call them from any of the client side SDKs mentioned in that documentation link

CodePudding user response:

See also the examples in the Firebase Functions Swift QuickStart

  • Related