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