i have a firebase function. I want to make increment ServerValue here. but it doesn't work it gives an error. where did i go wrong? I am attaching the console screenshot below. (note : I don't know javascript very well, there may be errors in java codes as well.)
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const database = admin.database();
async function sirala(dil, lig, oyunTipi) {
await database.ref("/turnuvalar/" + dil + "/" + lig + "/" + oyunTipi + "/users/").once("value").then(async function(snap) {
const users = [];
const updatesLig = {};
const refLig = database.ref().child("ligler").child(dil).child(lig).child("users");
snap.forEach((childSnap) => {
const id = childSnap.key;
const puan = childSnap.child("puan").val();
const serverTime = childSnap.child("serverTime").val();
users.push(new Person(id, puan, serverTime));
});
users.sort((a, b) => parseFloat(a.serverTime) - parseFloat(b.serverTime));
for (let i = 0; i < users.length; i++) {
let group = [];
const grupSayi = 3;
if (i < grupSayi) {
group = users.slice(0, grupSayi);
} else {
group = users.slice(i + 1 - grupSayi, i + 1);
}
group.sort((a, b) => parseFloat(b.puan) - parseFloat(a.puan));
const index = (element) => element.id === users[i].id;
const sira = (group.findIndex(index) + 1);
// const increment = database.ServerValue.increment(kupaAdet(sira));
const kupa = {
puan: database.ServerValue.increment(1), /// ??? ERROR IS THERE ???
};
updatesLig[users[i].id + "/"] = kupa;
console.log("user id " + users[i].id + " kupa - " + kupaAdet(sira) + " sira - " + sira);
for (let g = 0; g < group.length; g++) {
console.log("grup : " + (i + 1) + " - " + group[g].id + " - " + group[g].puan + " - " + group[g].serverTime);
}
}
await refLig.update(updatesLig);
return null;
}).catch((error) => {
console.log(error);
});
}
It looks like your database
is an instance of firebase.database()
(note the parentheses in there). The ServerValue
property is defined on firebase.database
without parentheses, so that explains why the increment
cannot be found.
You'll want to use either admin.database.ServerValue.increment(1)
or firebase.database.ServerValue.increment(1)
, depending on how you import the Admin SDK.