gpt3-twitter-bot icon indicating copy to clipboard operation
gpt3-twitter-bot copied to clipboard

SyntaxError: await is only valid in async functions

Open tobymac208 opened this issue 3 years ago • 8 comments

"Step 1" fails with error "SyntaxError: await is only valid in async functions and the top level bodies of modules":

exports.auth = functions.https.onRequest((request, response) => {
  const { url, codeVerifier, state } = twitterClient.generateOAuth2AuthLink(
    callbackURL,
    { scope: ['tweet.read', 'tweet.write', 'users.read', 'offline.access'] }
  );

  // store verifier
  await dbRef.set({ codeVerifier, state });

  response.redirect(url);
});

Please help.

tobymac208 avatar Feb 03 '22 19:02 tobymac208

But is supported in Node version later 14

codewithpom avatar Feb 04 '22 02:02 codewithpom

Try adding "async" in front of the function keyword if you're using an older version of node js.

Edit : sorry, you shouldn't add "async" before the function keyword as we are using another arrow function after that. So, the correct place to add async is before (request, response) => {}

shaheerk1 avatar Feb 04 '22 03:02 shaheerk1

But is supported in Node version later 14

v16.13.1 Still Problem ...

arifmamon avatar Feb 04 '22 05:02 arifmamon

I am facing the same issue. Any solutions yet ?

AkshatRastogi-1nC0re avatar Feb 04 '22 09:02 AkshatRastogi-1nC0re

Faced The Same Problem, add async after "onRequest("

exports.auth = functions.https.onRequest(async (req, res) => {

PixelPasta avatar Feb 05 '22 03:02 PixelPasta

I'm new to javascript. Would someone mind explaining why only step 1 needs async? Do they others need it to in the arrow functions?

JamesHWade avatar Feb 06 '22 13:02 JamesHWade

I had to add async to all 3 functions for it to work

PixelPasta avatar Feb 07 '22 01:02 PixelPasta

I'm having the same issue, I'll try adding async, but if someone knows how to change the project version of Node, that would be super helpful. I'm using N to version manage node, and v12 isn't one of the versions I have installed. When I run node --version I get the correct version. I'm assuming it's somewhere in the Firebase settings that needs to change, but I can't find where to do it.

coffeepostal avatar Feb 08 '22 04:02 coffeepostal