首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在云函数中调用云函数并包含数据

如何在云函数中调用云函数并包含数据
EN

Stack Overflow用户
提问于 2021-07-05 01:56:25
回答 2查看 215关注 0票数 1

我试图从另一个云函数中调用云函数"startTimer“,并在调用函数"startTimer”时包含数据。

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

代码语言:javascript
复制
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('startTimer');
callable.call({"gameId": MyFirebase.gameId});

然后,通过执行以下操作,我能够检索云函数"startTimer“中的数据:

代码语言:javascript
复制
exports.startTimer = functions.https.onCall(async (data, context) => {
  const gameId = data.gameId;

如何在另一个云函数中调用函数"startTimer“并包含数据(gameId)?此外,我将如何做到这一点,以便数据的格式/结构与上面的代码片段(data.gameId)完全相同?我试过这样的方法,但不管用:

代码语言:javascript
复制
fetch("https://{LOCATION}-{PROJECT-ID}.cloudfunctions.net/startTimer", {
    method: "POST",
    body: JSON.stringify({"gameId": gameId}),)};

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

我对javascript/typescript非常陌生,所以任何帮助/参考都是非常感谢的:)预先谢谢!

EN

回答 2

Stack Overflow用户

发布于 2021-07-05 09:49:01

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

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

代码语言:javascript
复制
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参数以添加回调的任何延迟。

票数 1
EN

Stack Overflow用户

发布于 2021-07-05 07:21:31

我可以看到两种可能性:

1.按照协议调用它为HTTP端点

正如您在文档中所读到的,“云函数的https.onCall触发器是具有请求和响应特定格式的HTTPS触发器。”

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

2.使用额外的Pub/Sub云功能

您可以在一个函数中重构业务逻辑,该函数是从两个云函数调用的:已经存在的可调用云函数或Pub/Sub触发云功能。与异步业务逻辑和异步/等待的使用类似的内容:

代码语言:javascript
复制
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;
    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68250048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档