javascript - 异步/等待在 Google cloud functions 中不起作用

我在 Firebase 上使用 google cloud functions 和云消息传递。但是,当我尝试使用 Async/Await 函数时,出现错误:error Parsing error: Unexpected token => I work with Node.js v16。

这是我的代码:

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

回答1

getDeviceTokensPromise 是否返回 Promise?如果是这样,那条线应该是

const tokensSnapshot = await getDeviceTokensPromise()

相似文章

最新文章