ios - 如何从HTTPS云函数返回错误?

标签 ios firebase swiftui google-cloud-functions

从 iOS 应用程序调用云函数如下所示:

var functions = Functions.functions()
functions.httpsCallable(name).call(data){ (result, error) in
    // Code here
}

如何从我的云函数发送错误,以便在上面的代码中拾取该错误?例如:

云功能:

exports.joinGame = functions.https.onCall((data, context) => {
     return Error("The game is full")
})

SwiftUI:

var functions = Functions.functions()
functions.httpsCallable("joinGame").call(data){ (result, error) in
    print(error) // "The game is full"
}

最佳答案

我仍然不确定你想在哪里返回错误...但这里有一些可能的处理变体:

@State private var error: Error?

var body: some View {
   VStack {
     Text("Wait for Error")
       .onAppear {
         functions.httpsCallable("joinGame").call(data){ (result, error) in
            print(error) // "The game is full"
            DispatchQueue.main.async {
               self.error = error
            }
         }
     }
     if nil != error {
        Text("Got error!")   // << do anything with error here
     }
   }
   .onChange(of: error) { newError in
                            // << or here 
   }
}

关于ios - 如何从HTTPS云函数返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64983057/

相关文章:

ios - AVAudioPlayer Swift 3 不播放声音

firebase - 为嵌套数据添加 .indexOn

ios - 如何从最新到最旧的顺序从 Firebase 获取图像?

ios - SwiftUI View 修改器 : How to create view modifier functions without using View extensions?

ios - Apple Wallet通过http url通过本地开发

iphone - MFMailComposeViewController : gives EXC-BAD-EXCESS when dismiss it from parent view

ios - 如何使用swift在谷歌驱动器中创建文件夹和文件

javascript - 如何为 SA 的预定功能编写自定义时区

ios - SwiftUI 误导性错误 : 'Int' is not convertible to 'CGFloat'

ios - 向VStack添加左边框