I'm facing an issue trying to retrieve the parent node key from the snapshot. The function below successfully returns the correct node data when I print snapshot.val()
, indicating that the problem is not with the query itself. However, I am struggling to find a way to extract the key of the returned snapshot.
/games
|--{game_id} ==> need to fetch this ID
|---"alias":"123456" ==> retrieved from the queried snapshot
|---"players":...
|..... // other children of game_id
Below is the cloud function code:
export const getGameIDFromCode = functions.https.onCall((data, context) => {
if (context.auth == null) {
throw new functions.https.HttpsError('permission-denied', 'You are not authorized to use this feature');
}
const code = data.code;
const gamesRef = db.ref("/games");
return gamesRef.orderByChild("alias").equalTo(code).once("value").then(snapshot => {
if (snapshot.ref.parent != null) {
// Attempted methods like snapshot.key and snapshot.ref.key without success
return snapshot.ref.parent.key;
} else {
return "Unable to locate game_id for the provided code";
}
}).catch(error => {
return error;
});
});