在 Firebase 函数中排队任务时出现“错误:队列不存在”

sve*_*ors 4 javascript node.js firebase google-cloud-functions

我有一个在数据库更新时触发的函数:

exports.eventAddedTrigger = functions
  .region('europe-west6')
  .firestore
  .document('users/{user_id}/events/{event_id}')
  .onCreate(async (snap, context) => {
    const event = snap.data();

    if (event) {
      const { user_id, event_id } = context.params;
      const queue = getFunctions().taskQueue('enrol');
      const signupDate = DateTime.fromSeconds(event.signupDate.seconds).minus({minutes: 2});
      const now = DateTime.local({zone: 'Europe/Zurich'})
      let scheduleDelaySeconds = Math.floor(signupDate.diff(now, 'seconds').seconds);
      if (scheduleDelaySeconds < 0) {
        scheduleDelaySeconds = 10;
      }
      functions.logger.info(`Scheduling enrollment for ${signupDate.toISO()} in ${scheduleDelaySeconds} seconds`);
      await queue.enqueue(
        { user_id, event_id },
        {
          scheduleDelaySeconds
        }
      )
    }
  });
Run Code Online (Sandbox Code Playgroud)

这个函数触发得很好,但是当涉及到入队时,我总是收到以下错误

Error: Queue does not exist
Run Code Online (Sandbox Code Playgroud)

无论我是在模拟中还是在生产中运行该函数。

注册函数如下所示:

exports.enrol = functions
  .region('europe-west6')
  .runWith({
    timeoutSeconds: 540,
    memory: '1GB',
  })
  .tasks
  .taskQueue()
  .onDispatch(async (data) => {
    const { user_id, event_id } = data.body;
    await _enrol(user_id, event_id, db);
    functions.logger.info(`Enrolled user ${user_id} to event ${event_id}`);
  });
Run Code Online (Sandbox Code Playgroud)

据我所知,我已正确初始化我的应用程序:

initializeApp({
  serviceAccountId: process.env.FIREBASE_SERVICE_ACCOUNT_ID,
});
Run Code Online (Sandbox Code Playgroud)

我必须在其他地方注册队列吗?

小智 9

我为自己想出了这个办法。如果您的函数使用非默认区域(即不是 us-central1),则需要指定包括目标区域的队列名称。\n架构在此处定义:https: //github.com/firebase/firebase-管理节点/blob/master/src/utils/index.ts#L293

\n

因此,请像这样使用您的入队函数:

\n
await\xc2\xa0this.functions \n    .taskQueue<INotification<any>[]>(`locations/${region}/functions/${queueName}`) \n    .enqueue(data);\n
Run Code Online (Sandbox Code Playgroud)\n