1

我正在尝试使用以下代码(带有所需的项目信息)安排一个谷歌云任务在指定的时间段(比如 60 秒)后重复一次。

async function scheduele() {
        const expiresIn = 60
        const expirationAtSeconds = TIME / 1000 + expiresIn
        const project = JSON.parse(process.env.FIREBASE_CONFIG).projectId
        const location = LOCATION
        const queue = QUEUE_NAME

        const tasksClient = new CloudTasksClient()
        const parent = await tasksClient.queuePath(project, location, queue)

        const url = "https://" + location + "-" + project + ".cloudfunctions.net/funcA"
        const base64String = Buffer.from(JSON.stringify(PAYLOAD)).toString('base64')

        const task = {
            "httpRequest": {
                "httpMethod": "POST",
                "url": url,
                base64String, 
                "headers": {
                    "Content-Type": "application/json"
                },
            }, 
        }

        task.scheduleTime = {
            seconds: expirationAtSeconds,
        };

        const request = {parent, task}
        const [response] = await tasksClient.createTask(request);
        return Promise.resolve({task: response.name})
}

我试图让它调用返回处理程序 funcA

exports.funcA = functions.https.onRequest(async (req, res) => {
    console.log(req.body)
    try {
        res.status(200).send("ok.")
        return; 
    } catch(error) {
        res.status(error.code).send(error.message);
        console.error(error.message)
    }
})

我可以使用来自谷歌云调度程序控制台的 JSON 调用该函数,但是,每次我触发 schedule() 时,我都会在 console.log(req.body) 甚至打印之前在 JSON 错误中得到一个意外的令牌“-”。我不确定为什么我可以从仪表板毫无问题地调用该函数,但是当我使用相同的 JSON 并从云函数触发它时,我会收到此错误。

谢谢!

PS 我检查了队列是否存在,npm 包都是最新版本,等等。

编辑:

PROMISE 是一个 JSON,其中包含一个沿 {"key": "somestring"} 行的条目。我还能够在谷歌云函数测试器中成功调用提交以下 JSON 的函数。

 "httpRequest":{
      "httpMethod":"POST",
      "url":"functionURL",
      "body":"LU1HUE4tZ3JBMlhqV3pXR0hrd0s=",
      "headers":{
         "Content-Type":"application/json"
      }
   },
   "scheduleTime":{
      "seconds":1599425744.055
   }
}

确切的错误是:

SyntaxError: Unexpected token - in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (/layers/google.nodejs.functions-framework/functions-framework/node_modules/body-parser/lib/types/json.js:158:10)
    at parse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/body-parser/lib/types/json.js:83:15)
4

0 回答 0