【问题标题】:How to create a pubsub topic within firebase functions如何在 firebase 函数中创建 pubsub 主题
【发布时间】:2019-02-25 07:35:13
【问题描述】:

我想通过 Firebase 云功能创建主题。我已经创建了这个,它似乎确实有效:

const init = () => {
    const topicName = "check-in-order";
  pubsub
    .createTopic(topicName)
    .then(results => {
      const topic = results[0];
            console.log(`Topic ${topicName} created.`);
            return;
    })
    .catch(err => {
            console.error("ERROR on init:", err);
            return;
    });
};

init()

但这显然每次都会运行。虽然它有效并且我可以简单地忽略错误,但我想正确地做到这一点。其他人有没有机会成功地做到这一点?

谢谢1

【问题讨论】:

  • 为什么需要在函数中创建它?只需在其他地方创建一次,然后在函数中尽可能多地使用它就足够了吗?
  • 我知道我应该这样做,但你是怎么做到的?您的代码不会每次都部署吗?导致同样的问题?顺便说一句,感谢您的反馈!
  • 您只需在本地 Node.js 脚本中运行一次代码即可创建主题。然后,您部署到 Cloud Functions 的代码不包含有关创建主题的任何内容,而只是订阅它。
  • 您不需要重新部署。 pubsub 主题是一种持久性资源,在您创建后会一直存在。

标签: firebase google-cloud-functions google-cloud-pubsub


【解决方案1】:

你可以在你的函数之外create a topic,然后使用它。最简单的方法可能是使用gcloud command line tool

gcloud pubsub topics create name-of-topic

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 2021-07-30
    • 2021-06-02
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2021-03-17
    • 2021-06-21
    • 2020-10-30
    相关资源
    最近更新 更多