0

I'm using node version 10.15.3, npm version 6.9.0, VS code, and firebase-functions version 2.2.0 on a windows pc. Adding async/await to my app.post() function causes this:

 Function failed on loading user code. Error message: Code in file 
 index.js can't be loaded.
 Is there a syntax error in your code?
 Detailed stack trace: /user_code/index.js:31
 app.post('/pay-now', async (req, res) => {
                       ^

 SyntaxError: Unexpected token (
     at createScript (vm.js:56:10)
     at Object.runInThisContext (vm.js:97:10)
     at Module._compile (module.js:549:28)
     at Object.Module._extensions..js (module.js:586:10)
     at Module.load (module.js:494:32)
     at tryModuleLoad (module.js:453:12)
     at Function.Module._load (module.js:445:3)
     at Module.require (module.js:504:17)
     at require (internal/module.js:20:19)
     at getUserFunction (/var/tmp/worker/worker.js:439:24)

Here's my app.post():

app.post('/pay-now', async (req, res) => {
    // charge user's card
    const charge = await makeCharge(req, res)
    // store order info in database, returns address of order
    const address = await storeOrder(req, res, charge.id)
    // send email to customer
    await emailHandler.sendCustomerEmail(req, res)
    // send email to company letting them know they have a new order
    await emailHandler.sendLTEmail(req, res, address, true)
    return res.sendStatus(200)
})

I tried removing the async & await's in app.post(), but then I get the same error at the first use of async in the function makeCharge. Any idea on what could be wrong?

Zobia Kanwal
  • 4,085
  • 4
  • 15
  • 38
user11210907
  • 13
  • 1
  • 2

2 Answers2

1

Currently, the default runtime for Cloud Funtions is node 6, which doesn't support async/await. You will need to edit your package.json to target the node 8 runtime, which uses a version of JavaScript that does have async/await.

Set the version by adding an engines field to the package.json file that was created in your functions/ directory during initialization. For example, if you prefer to use only version 8, edit package.json to add this line:

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

If you have already deployed a version of the function, you will need to delete it and deploy again after this configuration is in place.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
-1

maybe you should take a look here : Syntax for async arrow function

There is no problem with your module, I think the arrow function is not well written.

EDIT :

I don't know much about arrow function, can you try to use a "normal function" something like :

  app.post('/pay-now', async function Myfunction (req, res) {
  // do something
}