【问题标题】:How to call a cloud function within a cloud function AND include data如何在云函数中调用云函数并包含数据
【发布时间】:2021-07-05 01:56:25
【问题描述】:

我正在尝试从另一个云函数调用云函数“startTimer”,并在调用函数“startTimer”时包含数据

例如,在我的客户端,很容易调用函数“startTimer”并通过编写以下代码来包含数据(颤振代码):

HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('startTimer');
callable.call({"gameId": MyFirebase.gameId});

然后我可以通过执行以下操作在我的云函数“startTimer”中检索数据:

exports.startTimer = functions.https.onCall(async (data, context) => {
  const gameId = data.gameId;

如何在另一个云函数中调用函数“startTimer”并包含数据 (gameId)?此外,我将如何做到这一点,以便数据的格式/结构与上面的代码 sn-p (data.gameId) 一样? 我尝试了类似的方法,但它不起作用:

fetch("https://{LOCATION}-{PROJECT-ID}.cloudfunctions.net/startTimer", {
    method: "POST",
    body: JSON.stringify({"gameId": gameId}),)};

“取”来自 const fetch = require("node-fetch");

我对 javascript/typescript 很陌生,因此非常感谢任何帮助/参考 :) 提前谢谢!

【问题讨论】:

    标签: node.js firebase google-cloud-functions


    【解决方案1】:

    我可以看到两种可能性:

    1。按照协议将其称为 HTTP 端点

    正如您将在 doc 中看到的那样,“Cloud Functions 的 https.onCall 触发器是一个 HTTPS 触发器具有特定格式的请求和响应。”

    因此,您可以从另一个云函数调用 Callable Cloud Function,例如Axios 或 fetch。

    2。使用额外的 Pub/Sub Cloud 函数

    您可以在一个函数中重构您的业务逻辑,您可以从两个云函数调用该函数:已经存在的可调用云函数或a Pub/Sub triggered Cloud Function。大致如下,具有异步业务逻辑和 async/await 的使用:

    exports.startTimer = functions.https.onCall(async (data, context) => {
        try {
            const gameId = data.gameId;
            const result = await asyncBusinessLogic(gameId);
            return { result: result }
        } catch (error) {
            // ...
        }
    });
    
    exports.startTimerByPubSubScheduler = functions.pubsub.topic('start-timer').onPublish((message) => {
    
        try {
            const gameId = message.json.gameId;
            await asyncBusinessLogic(gameId);
            return null;
        } catch (error) {
            // ...
            return null;
        }
    });
    
    
    async function asyncBusinessLogic(gameId) {
        
        const result = await anAsynchronousJob(gameId);
        return result;
        
    }
    

    【讨论】:

    • 嘿@JeffreyWolberg,您有时间查看建议的解决方案吗?
    • 您好,非常感谢您的回答。很有帮助
    • 嗨@JeffreyWolberg,很高兴我的回答很有帮助。你可以接受它,见What should I do when someone answers my question?。谢谢!
    【解决方案2】:

    除了@Renaud,您还可以使用Google Cloud Tasks 来调用您的HTTP 函数。这样做的主要好处是您可以从第一个函数返回响应,而无需等待第二个函数完成。这是一件小事,但我发现它在很多情况下都有用。

    您可以将您可能需要的任何数据传递给 GCloud 任务的主体,这样您就不必再次从第二个函数调用数据库。

    exports.firstFunctions = functions.https.onCall(async (data, context) => {
      // Functions logic
      await addCloudTask(/*params*/)
      //Return the response
    })
    
    
    const addCloudTask = async (queue, url, method, inSeconds, body) => {
      const project = "<project-id>"
      const location = "us-central1"
      const parent = gCloudClient.queuePath(project, location, queue)
    
      const [response] = await gCloudClient.createTask({
        parent,
        task: {
          httpRequest: {
            httpMethod: method,
            url,
            body: Buffer.from(JSON.stringify({body})).toString("base64"),
            headers: {
              "Content-Type": "application/json"
            }
          },
          scheduleTime: {
            seconds: inSeconds + (Date.now() / 1000)
          }
        }
      });
      return
    }  
    

    您还可以传递inSeconds 参数来为回调添加任何延迟。

    【讨论】:

    • 谢谢,任务很有帮助
    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2021-02-14
    • 2019-07-28
    • 1970-01-01
    • 2019-06-01
    • 2021-09-12
    • 2017-08-04
    相关资源
    最近更新 更多