通过Firebase函数调度Google Cloud Task时出错,但从任务仪表板进行调度时没有错误

问题描述

我正在尝试使用以下代码(带有所需的项目信息)安排在指定的时间段(例如60秒)后再次执行一次Google云任务。

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)
    }
})

我能够从Google Cloud Scheduler控制台使用JSON调用函数,但是,每次触发scheduele()时,都会在console.log(req.body)之前在JSON错误中收到意外的令牌“-”。甚至打印。我不确定为什么可以从仪表板毫无问题地调用函数,但是当我使用相同的JSON并从云函数触发它时,会出现此错误

谢谢!

P.S。我检查了队列是否存在,npm软件包都是最新版本,等等。

编辑:

PROMISE是一种JSON,其中包含一个与{“ key”:“ somestring”}相似的条目。我还能够成功调用在Google云函数测试器中提交以下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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)