简体   繁体   English

如何使用我在firebase函数中设置的参数调用该函数

[英]How do I call the function with a parameter I set up in firebase functions

I've deployed this code to my firebase functions project: 我已将此代码部署到我的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)
    })
})

Which takes in a email parameter that it gets from the user's input on my app. 它接收来自用户在我的应用程序上的输入的电子邮件参数。 My app's code looks like this: 我的应用程序代码如下所示:

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
                }
            }

When I run the app and when that function is called, it seems to do nothing, no error message is shown and no data has been sent back. 当我运行应用程序并且调用该函数时,它似乎什么都不做,没有显示错误消息,也没有发回任何数据。 What am I missing? 我错过了什么?

Update: I went to the logs and nothing has happened in there as if the function was never called. 更新:我去了日志,没有发生任何事情,好像从未调用过该函数。

You are actually mixing up HTTP Cloud Functions and Callable Cloud Functions : 您实际上正在混合HTTP云功能可调用云功能

You Cloud Function code corresponds to an HTTP one but the code in your front-end seems to call a Callable one. 云功能代码对应于HTTP功能,但前端的代码似乎称为可调用代码。

You should adapt one or the other, most probably adapt your Cloud Function to a Callable one, along the following lines: 您应该根据以下几点调整其中一个,最有可能使您的云功能适应可调用的功能:

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 };
  })
});

Have a look at the doc for more details, in particular how to handle errors. 有关更多详细信息,请查看文档,特别是如何处理错误。 The doc is quite detailed and very clear. 该文件非常详细,非常清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JavaScript 设置函数参数? - How do i set a function parameter with JavaScript? 如何在地图功能内的每个列表项上建立呼叫 - How do I set up a call on each list item within map function 如何使用razor语法设置JavaScript变量或调用函数? - How do I set javascript variables or call functions with the razor syntax? 如何解决“未定义 Firebase”错误? 我正在尝试在最基本的级别设置 Firebase - How do I resolve a "firebase is not defined" error? I'm trying to set up Firebase at the most basic level Titanium如何从函数名称数组中调用函数? - Titanium How do i call functions from an array of function names? 如何在Javascript中正确调用匿名函数? - How do I correctly call a functions of anonymous function in Javascript? Javascript 如何从函数名称数组调用函数 - Javascript How do i call functions from an array of function names 如何调用此JS函数并传递参数? - How do I call this JS function and pass a parameter through? 我如何在jQuery的ajax函数调用中发送参数 - How do i send parameter in ajax function call in jquery 我是否应该在 Firebase 云函数中等待 Firestore 设置调用 - Should I await a Firestore set call in Firebase cloud functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM