0

Part of a Google Firebase Cloud Function, where I try to run:

CODE#1: [ firebase deploy] cron job to event-notify-about-upcoming-event.js.

note: /*[L47]*/-> "points to error line Number"

const pushToChannelMembers = async (channelDocument, groupId, groupName, isTomorrow = false) => {
  const channelUsers = await usersInChannel(groupId, channelDocument.id);  /*[Line47]*/
  const channelData = channelDocument.data();

  channelDocument.ref.update({
    notified_members: true
  });

  for (const userSnapshot of channelUsers.docs) {
    const uid = userSnapshot.data().uid;
    const userDoc = await getUser(uid);
    const locale = userDoc.data().locale;

    const localizedTimeString = timeStringForDate(channelData.start_date, channelData.timezone_seconds_offset);
    const title = getNotificationTitleForUpcomingEvent(locale, groupName);
    const body = getNotificationBodyForUpcomingEvent(locale, channelData.name, isTomorrow ? null : localizedTimeString);

    await pushToUser(userDoc.id, userDoc.data().token, title, body, "", groupId, channelDocument.id);  /*[Line54]*/
  }
}
:
const pushToEventChannelMembers = async (eventChannelDocs, groupId, groupName, isTomorrow) => {
  for (const channelDocument of eventChannelDocs) {
    const channel = channelDocument.data();

    if (channel.notified_members !== true) {
      await pushToChannelMembers(channelDocument, groupId, groupName, isTomorrow); /*[line110]*/
    }
  }
}

Result:

47:21 error Unexpected await inside a loop no-await-in-loop

54:5 error Unexpected await inside a loop no-await-in-loop

110:7 error Unexpected await inside a loop no-await-in-loop

CODE#2: [ firebase deploy] cloud function to reaction-push.js.

     for (const key in reactionAfter) {
            // A new reaction was added
            if (!Object.keys(reactionBefore).includes(key)) {
                console.log('New Emoji reaction added to message');

                // get the list of users that joined the channel
                const users = await usersInChannel(context.params.groupId, context.params.channelId); /*[Line22]*/
                // Check if the author is still part of the channel
                if (!users.docs.some((user) => {
                    return authorUid === user.id;
                })) {
                    console.log('Author left the channel');
                    return;
                }

                // Update read flag for author.
                await flagChannelUnread(context.params.groupId, context.params.channelId, author.data().uid); /*[Line32]*/

                const val = reactionAfter[key];
                const emoji = val.emoji;
                const username = val.user_name;
                const token = author.data().token;

                // If the user has a cloud messaging token registered
                if (token !== null) {
                    const title = getNotificationTitleForReaction(username, groupName, channelName);
                    const body = getNotificationBodyForReaction(author.data().locale, emoji);
                    console.log('Sending notification to user ' + author.data().name + ' with text: ' + body);
                    const message = getMessage(
                        token,
                        title,
                        body,
                        { type: "reaction" });
                    await sendPush(message); /*[Line49]*/
                }
            }
        }

Result:

22:27 error Unexpected await inside a loop no-await-in-loop

32:13 error Unexpected await inside a loop no-await-in-loop

49:17 error Unexpected await inside a loop no-await-in-loop

Please help by writing the right syntax to fix these errors

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
kash71
  • 1
  • 1

0 Answers0