【问题标题】:Receiving a region error deploying Firebase Functions with Google Cloud Task client使用 Google Cloud Task 客户端部署 Firebase 函数时收到区域错误
【发布时间】:2021-12-27 15:10:55
【问题描述】:

问题

当我部署导入 Google Cloud Tasks @google-cloud/tasks 的 Firebase Functions 时,我收到一个区域错误。

为了证明这一点,我包含了成功和不成功部署的代码。

成功

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

export const helloWorld = functions.region("europe-west3").https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

不成功

CloudTaskClientonDeletePostCancelTask 函数被添加到成功的代码中。

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
const {CloudTasksClient} = require("@google-cloud/tasks");

admin.initializeApp();
const tasksClient = new CloudTasksClient();

export const helloWorld = functions.region("europe-west3").https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

export const onDeletePostCancelTask = functions.region("europe-west3").database
    .ref("/one/{twoId}").onDelete(async (snapshot, context) => {
      const dogId = snapshot.key;
      const taskSnap = await snapshot.ref.parent?.parent?.child("three/" + twoId).get();
      const taskName = taskSnap?.val();
      console.log("Task name: ", taskName);
      return tasksClient.deleteTask({name: taskName});
    });

错误:

Error: There was an error deploying functions:
- Error Failed to create function helloWorld in region europe-west3
- Error Failed to create function onDeletePostCancelTask in region europe-west3

在 Firebase Functions 日志中,我发现以下内容:

Provided module can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '@google-cloud/tasks'
Require stack: 
- /workspace/lib/index.js 
...

更多信息

  • 我按照How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL) 中所述的安装过程进行操作。
  • 我的 Firebase 应用区域是 europe-west3。
  • 我的 Google Cloud 应用程序区域是欧洲西部。 (但是,在开始配置 Cloud Tasks 时,我使用 us-central1 区域创建了新项目,但由于我的项目实例包含欧洲西部区域,因此我切换到了它。)
  • 当我省略 .region("europe-west3") 时,错误仍然存​​在,只是使用 us-central1。
  • 我有两个package.json,第一个在../,第二个在../functions/。外层有@google-cloud/tasks 依赖:
{
  "dependencies": {
    "@google-cloud/tasks": "^2.4.2",
    "firebase-admin": "^10.0.0",
    "firebase-functions": "^3.16.0"
  }
}

【问题讨论】:

  • 你需要在../functions目录下的package.json文件中包含@google-cloud/tasks
  • 是的,这就是答案!谢谢!!

标签: typescript firebase google-cloud-functions google-cloud-tasks


【解决方案1】:

函数部署失败,因为在你的函数目录下找不到package.json中的@google-cloud/tasks,区域关注没有问题。如果您的函数中缺少依赖项,则无法将其部署到任何区域。

要修复它并在您的function/package.json 中添加@google-cloud/tasks 依赖项,请在您的function 目录中运行以下命令:

npm install @google-cloud/tasks

再次部署,应该会成功。

【讨论】:

  • 就是这样!这么小的细节,却浪费了这么多时间。谢谢
猜你喜欢
  • 2019-01-17
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
相关资源
最近更新 更多