0

I work with google cloud functions and cloud messaging on Firebase. However, when I try to use Async/Await functions, I have an error: error Parsing error: Unexpected token => I work with Node.js v16.

Here is my code:

const functions = require("firebase-functions");
// const mess = require("firebase/messaging");
const admin = require("firebase-admin");

exports.sendListenerPushNotificationProductUpdate = functions.database
    .ref("Products/{product}/type/")
    .onUpdate(async (snapshot, context) => {

      ...

      const tokensSnapshot = await Promise.resolve(getDeviceTokensPromise);
      console.log("TOKEN: " + JSON.stringify(tokensSnapshot));
      // Check if there are any device tokens.
      if (!tokensSnapshot.hasChildren()) {
        return functions.logger.log(
            "There are no notification tokens to send to."
        );
      }
      
       ...

      // Listing all tokens as an array.
      const tokens = Object.keys(tokensSnapshot.val());
      // Send notifications to all tokens.
      const response = await admin.messaging().sendToDevice(tokens, payload);
      // For each message check if there was an error.
      const tokensToRemove = [];
      return Promise.all(tokensToRemove);
    });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Inv4si0n
  • 5
  • 2
  • I don't see a syntax error, but a couple things look weird: `await Promise.resolve(...` wouldn't do damage but would never be right. Same for `Promise.all([])`. Maybe post more complete code, and indicate the line number of the fat arrow syntax error. – danh May 18 '22 at 15:36
  • No there is any error but it does'nt recognised async/await functions. – Inv4si0n May 18 '22 at 15:39
  • Maybe I need a js file in my 'functions' folder ? – Inv4si0n May 18 '22 at 15:41
  • I believe you need an functions/index.js. The post talks about a syntax error. Is that not happening? – danh May 18 '22 at 15:50
  • My code is in the index.js, but the async/await is not recognised – Inv4si0n May 18 '22 at 15:54
  • it's not clear why you think that async/await is not recognized. It doesn't help that you're not showing the complete code that causes the problem. Please read about how to to create a [minimal complete example](https://stackoverflow.com/help/minimal-reproducible-example) and edit the question to narrow the scope of the problem. It would probably be best to also illustrate what does work, and explain exactly what you did to it that does not work the way you expect. – Doug Stevenson May 18 '22 at 15:57
  • I found the solution here: https://stackoverflow.com/questions/50285821/eslint-parsing-error-unexpected-token-function-with-async – Inv4si0n May 20 '22 at 12:47

1 Answers1

0

Does getDeviceTokensPromise return a Promise? If so, that line should just be

const tokensSnapshot = await getDeviceTokensPromise()
JTron
  • 13
  • 3