如何在 firebase 函数中创建 pubsub 主题

How to create a pubsub topic within firebase functions

我想通过 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

你可以create a topic outside your function, then use it. Easiest way might be with the gcloud command line tool:

gcloud pubsub topics create name-of-topic