0

I have an issue where I cant get the val() of myRef1Value without setting it to await. The issue is though when I do, I get this error Cannot use keyword 'await' outside an async function.

I am trying to increase the counter of all users by 1. Does anyone have any idea how I can fix this.

const functions = require("firebase-functions");

const admin = require("firebase-admin");
admin.initializeApp();

exports.scheduledFunction = functions.pubsub
    .schedule("* * * * *")
    .onRun(async (context) => {
      console.log("starting");
      const db = admin.database();
      const updates = {};

      const snapshot = await db.ref("Users").once("value");

      snapshot.forEach((childSnapshot) => {
        const myRef1Value = (await db
            .ref("Users/" + childSnapshot.key + "/counter").get());
        console.log(myRef1Value+"");
        const newValue = myRef1Value.val() + 1;
        console.log(newValue);
        updates["/Users/" + childSnapshot.key + "/counter"] = newValue;
      });
      return db.ref().update(updates);
    });
slideshowp2
  • 63,310
  • 57
  • 191
  • 365
  • You have await in the forEach. Checkout [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – Dharmaraj Feb 17 '22 at 04:53

0 Answers0