【问题标题】:How to make functions.httpsCallable("findItems").call(["lat": userLocation?.latitude, "long": userLocation?.longitude]) request?如何制作 functions.httpsCallable("findItems").call(["lat": userLocation?.latitude, "long": userLocation?.longitude]) 请求?
【发布时间】:2020-11-05 13:53:10
【问题描述】:

关于 firebase 函数的文档不足。httpsCallable("findItems").call() 用法。我已经在 firebase 上部署了函数:

exports.findItems = functions.https.onCall((data, context) => {
// Grab the long parameter.
functions.logger.log(data.long, typeof data.long);
functions.logger.log(data.lat, typeof data.lat);
const long = parseFloat(data.long);
const lat = parseFloat(data.lat);
functions.logger.log(long, typeof long);
functions.logger.log(lat, typeof lat);
    //...
}

我使用 functions.httpsCallable("findItems").call() 快速发出请求,但日志总是说没有正确的参数:

functions.httpsCallable("findItems").call(["lat": userLocation?.latitude, "long": userLocation?.longitude]) { (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]
                }
                // ...
                print(error)
                return
            }
            print(result?.data)
            do{
                let json =  try JSONDecoder().decode([ItemLocation].self, from:result?.data as! Data)
                print(json)
            }catch{

            }
            
            
            
//            if let text = (result?.data as? [String: Any])?

            
            //(result?.data as? [String: Any])?["text"] as? String {
            //self.resultField.text = text
        }

您可以从https://firebase.google.com/docs/functions/callable找到官方文档

-----更新---------

我已经通过调试尝试了所有方法,传递的参数始终未定义,但我遵循官方文档中的每一步。 我做了更改并使用了 onCall 但它仍然不起作用?

【问题讨论】:

    标签: swift firebase google-cloud-functions


    【解决方案1】:

    您将 HTTP 函数与可调用函数混为一谈。您的客户端代码正在尝试调用可调用函数,但您的函数本身被定义为 HTTP 函数。这根本行不通。您应该查看可调用函数的文档并使用functions.https.onCall 而不是functions.https.onRequest 来定义您的函数。它们是完全不同的 API。

    【讨论】:

    • 您好,我已经更改了部署的功能,但仍然无法获取参数?有什么建议吗?
    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2012-10-22
    相关资源
    最近更新 更多