Google Tasks Api - 错误:3 INVALID_ARGUMENT:请求包含无效参数

SIM*_*SAL 5 google-tasks-api firebase google-cloud-firestore

我编写了以下代码,以便在每当将新项目添加到 Firestore 中的集合时创建一个任务。

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'

const { CloudTasksClient } = require('@google-cloud/tasks')

exports.moveActivityFromPlanToRecord = () =>
    functions
    .region('europe-west1')
    .firestore.document('Users/{userId}/Activities/{activityId}')
        .onCreate(async snapshot => {

            const moveTime = snapshot.data()! as MoveTime

            if (!moveTime || !moveTime.dueTime) {
                console.log("DueTime is empty or null: \n" + moveTime)
                return
            }


            // Get the project ID from the FIREBASE_CONFIG env var
            const project = JSON.parse(process.env.FIREBASE_CONFIG!).projectId
            const location = 'europe-west1'
            const queue = 'activityDateEventChecker'

            //queuePath is going to be a string that uniquely identifes the task
            const tasksClient = new CloudTasksClient()
            const queuePath: string =
                tasksClient.queuePath(project, location, queue)

            // URL to my callback function and the contents of the payload to deliver
            const url = `https://${location}-${project}.cloudfunctions.net/activityDateEventCheckerCallback`
            const docPath = snapshot.ref.path
            const dueTime = moveTime.dueTime
            const payload: MoveTaskPayload = { docPath, dueTime }

            console.log(payload)

            // build up the configuration for the Cloud Task
            const task = {
                httpRequest: {
                    httpMethod: 'POST',
                    url: url,
                    body: Buffer.from(JSON.stringify(payload)).toString('base64'),
                    headers: {
                        'Content-Type': 'application/json',
                    },
                },
                scheduleTime: {
                    seconds: moveTime.dueTime / 1000
                }
            }

            // enqueue the task in the queue
            return tasksClient.createTask({ parent: queuePath, task: task })
        })


interface MoveTime extends admin.firestore.DocumentData {
    dueTime?: number
}
interface MoveTaskPayload {
    docPath: string,
    dueTime: number
}
Run Code Online (Sandbox Code Playgroud)

当该函数被触发时(当一个新的“活动”被添加到集合中时),它会抛出以下错误:

错误:3 INVALID_ARGUMENT:请求包含无效参数

这里可能有什么问题?
顺便说一句,方法中的最后一行应该是return任务吗await


编辑:完全相同的代码现在可以工作,无需我更改任何内容!我只是为了好玩才将它与 Termux 应用程序一起部署,之后它就开始工作了!

Sha*_*ert 0

我的猜测是您当前的问题源于: `exports.moveActivityFromPlanToRecord = () => ...

如果删除该() =>部分,那么当 moveActivityFromPlanToRecord调用该函数时,预期参数将需要与onCreate()预期相匹配。

这不应该为您解决所有问题onCreate(),因为需要两个参数(从文档而不是源代码中function onCreate(snapshot: DataSnapshot, context: EventContext): PromiseLike<any> | any获取,因为我在移动设备上)。这意味着该函数无法在当前实现中接收参数。onCreate()

这里可能有什么问题?\ 顺便说一句,方法中的最后一行应该返回任务,还是等待它?几乎可以肯定您需要返回 aPromiseString。这是您应该了解所有后台 Google Cloud 功能的基本了解。

可能还有其他问题,但这应该有助于解决您的要求。