【问题标题】:How do I call the function with a parameter I set up in firebase functions如何使用我在 firebase 函数中设置的参数调用函数
【发布时间】:2019-09-14 02:19:09
【问题描述】:

我已将此代码部署到我的 firebase 函数项目中:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()

export const getEmail = functions.https.onRequest((request, response) => {
    var from = request.body.sender;
    admin.auth().getUserByEmail(from)
    .then(snapshot => {
        const data = snapshot.toJSON()
        response.send(data)
    })
    .catch(error => {
        //Handle error
        console.log(error)
        response.status(500).send(error)
    })
})

它接受从用户在我的应用程序上输入的电子邮件参数。我的应用程序代码如下所示:

Functions.functions().httpsCallable("https://us-central1-projectname.cloudfunctions.net/getEmail").call(email) { (result, error) in
                if let error = error as NSError? {
                    if error.domain == FunctionsErrorDomain {
                        //email isnt taken
                        let code = FunctionsErrorCode(rawValue: error.code)
                        let message = error.localizedDescription
                        let details = error.userInfo[FunctionsErrorDetailsKey]
                        print(code, message, details)
                    }
                    // ...
                }
                if let text = (result?.data as? [String: Any])?["text"] as? String {
                    // email taken
                }
            }

当我运行应用程序并调用该函数时,它似乎什么都不做,没有显示错误消息,也没有发回任何数据。我错过了什么?

更新:我查看了日志,那里没有发生任何事情,好像从未调用过该函数。

【问题讨论】:

    标签: javascript swift firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    您实际上是在混淆HTTP Cloud FunctionsCallable Cloud Functions

    您的 Cloud Function 代码对应于 HTTP 代码,但您前端的代码似乎调用了 Callable 代码。

    您应该按照以下思路调整其中一个,最有可能将您的云函数调整为可调用函数:

    exports.getEmail = functions.https.onCall((data, context) => {
      const from = data.sender;
    
      return admin.auth().getUserByEmail(from)
      .then(userRecord => {
            const userData = userRecord.toJSON();
            return { userData: userData };
      })
    });
    

    查看文档了解更多详细信息,尤其是如何处理错误。文档非常详细,非常清晰。

    【讨论】:

    • 查看更新。另外,请查看文档了解更多详细信息,它非常详细且非常清晰。
    • 我厌倦了新代码,并且在它有 'const data = snapshot.toJSON()' 的行出现错误:'Shadowed name: 'data''
    • 试试const userData = snapshot.toJSON() return { userData: userData };
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2016-11-13
    • 2017-10-05
    • 2019-12-13
    相关资源
    最近更新 更多