1

I am trying to schedule a google cloud task to recur once after a specified period of time (say 60 seconds) using the following code (with the required project info).

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

I am trying to get this to call a return handler 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)
    }
})

I am able to call the function using a JSON from the google cloud scheduler console, however, every time I trigger scheduele(), I get an unexpected token "-" in JSON error before console.log(req.body) even prints. I am unsure why I can call the function with no problem from the dashboard, but when I use the same JSON and trigger it from a cloud function, I get this error.

Thanks!

P.S. I have checked that the queue exists, npm packages are all latest version, etc.

Edit:

PROMISE is a JSON with one entry along the lines of {"key": "somestring"}. I also was able to successfully invoke the function submitting the following JSON in the google cloud function tester.

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

The exact error is:

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)
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Please edit the question to show exactly what `PAYLOAD` is. It might also help to see what you are doing that invokes the function successfully. – Doug Stevenson Sep 06 '20 at 22:25
  • Thank you @DougStevenson, I really appreciate it. The Payload is a JSON with one key string pair, like {"key": "somestring"}. As for invoking the function successfully, submitting the following JSON in the google cloud function testing editor works. Perfectly. "httpRequest":{ "httpMethod":"POST", "url":"functionURL", "body":"LU1HUE4tZ3JBMlhqV3pXR0hrd0s=", "headers":{ "Content-Type":"application/json" } }, "scheduleTime":{ "seconds":1599425744.055 } } Let me know if there is other information I can provide. – Jared Geller Sep 07 '20 at 00:49
  • Please, edit your question and add the exact error message that you are facing, so we can have a glance of what might be causing the issue. – gso_gabriel Sep 07 '20 at 13:05
  • Thank you @gso_gabriel. I included the exact error message. – Jared Geller Sep 07 '20 at 16:40
  • 1
    I believe the error is related to your `JSON.parse`. As this similar case [here](https://stackoverflow.com/questions/14432165/uncaught-syntaxerror-unexpected-token-with-json-parse), it seems that you are trying to convert an object, which is causing the issue. Considering that, you should be using the object or try the solution [here](https://stackoverflow.com/a/52679388/12767257). Could you please take a look at these cases and check if they help you? – gso_gabriel Sep 09 '20 at 05:43

0 Answers0