My current challenge involves retrieving the list of users in my database when a specific field is updated. I aim to modify the scores of players based on the structure outlined below:
The Realtime Database Schema:
{
"users": {
"user1": {
"name": "abc",
"game": {
"gameId": "id1"
"score": 123
}
},
"user2": {
"name": "def",
"game": {
"gameId": "id1"
"score": 456
}
},
"user3": {
"name": "ghi",
"game": {
"gameId": "id1"
"score": 789
}
}
},
"leaderboard": {
"game": {
"gameId": "id1",
"players": [
"playerName": ...
"score": ...
]
}
}
Whenever a user updates their score, I trigger OnValueUpdate()
successfully and access the necessary data before and after the update. However, I struggle to retrieve the full list of users for comparison and leaderboard updates. Despite trying various approaches, all queries return null
.
Here are some attempts:
const admin = require('firebase-admin');
admin.initalizeApp();
export const func = OnValueUpdate(
{
ref: '/users/{userId}/games/{gameId}/score';
},
(event) => {
const db = admin.database();
db.ref('/users/{userId}')
.get()
.then( (result) => {
console.log(result.val()); // prints null
});
}
)
I also experimented with:
const functions = require('firebase-functions');
export const func ) OnValueUpdate(
{
ref: '/users/{userId}/games/{gameId}/score';
},
(event) => {
const db = functions.database
// and then access the ref with db.ref...
}
)
Yet, the results remain null
. Any suggestions on what else I could try?
While utilizing Firebase emulators locally, everything functions as expected.