According to the docs, I use this code to get the players in ascending order:
const db = getDatabase();
const playersByPoints = query(
ref(db, 'usersPoints'),
orderByChild('points'),
);
But when I console.log(playersByPoints) I get this:
"https://app-name.firebaseio.com/usersPoints"
Here is the database:
Am I missing something?
Thanks!
Your playersByPoints here is a Query object, which when logged returns the string of the linked database reference.
You need to actually invoke the query using either get(q) or onValue(q) to get the data you are looking for.
// one-off
const playersByPointsQuery = query(
ref(db, 'usersPoints'),
orderByChild('points'),
);
const playersByPointsQuerySnapshot = await get(playersByPointsQuery);
const playersByPoints = [];
playersByPointsQuerySnapshot.forEach(childSnapshot => {
playersByPoints.push({
...childSnapshot.val(),
_key: childSnapshot.key
});
});
// todo: do something with playersByPoints
or
// realtime listener
const playersByPointsQuery = query(
ref(db, 'usersPoints'),
orderByChild('points'),
);
const unsubscribe = onValue(
playersByPointsQuery,
{
next: (playersByPointsQuerySnapshot) => {
// new data available
const playersByPoints = [];
playersByPointsQuerySnapshot.forEach(childSnapshot => {
playersByPoints.push({
...childSnapshot.val(),
_key: childSnapshot.key
});
});
// todo: do something with playersByPoints
},
error: (err) => {
// error
// todo: handle
}
}
);