首页 > 解决方案 > 部署 Google Cloud Task Client 时出错

问题描述

我正在尝试使用 Google Cloud Tasks 创建任务,但遇到错误。

我的代码可以完美编译,但是当它到达 Cloud Tasks 初始化行时:

const data = snap.data();

let expirationInSeconds
if(data.booking_date_ts - Date.now() > 172800000){
    expirationInSeconds = (Date.now() + 172800000) / 1000
}else{
    expirationInSeconds = (Date.now() + 180000) / 1000
}

const project = JSON.parse(process.env.FIREBASE_CONFIG).projectId
const location = 'us-central1'
const queue = 'reminders'

const tasksClient = new CloudTasksClient() // This is the error line
const queuePath = tasksClient.queuePath(project, location, queue)

const url = `https://${location}-${project}.cloudfunctions.net/bookingExpirationCallback`
const docPath = snap.ref.path
const payload = { docPath }

const task = {
    httpRequest: {
        httpMethod: 'POST',
        url,
        body: Buffer.from(JSON.stringify(payload)).toString('base64'),
        headers: {
            'Content-Type': 'application/json',
        },
    },
    scheduleTime: {
        seconds: expirationInSeconds
    }
}

return await tasksClient.createTask({ parent: queuePath, task });

我收到以下错误:

错误:Node.js v10.0.0 是最低要求

这很奇怪,因为他们的文档说:

我们的客户端库遵循 Node.js 发布时间表。库与 Node.js 的所有当前活动和维护版本兼容。

我当前的 Node.js 版本是:v12.16.3

在此处输入图像描述

标签: node.jsgoogle-cloud-platformgoogle-tasks-api

解决方案


问题是我的 Node.js 版本设置为 12.16.3,但 process.version 是 8.3.2。

如何解决?

修改 package.json 引擎:

"engines": {
    "node": "8"
  },

  "engines": {
    "node": "10"
  },

推荐阅读