Firebase Cloud函数-实时数据库-获取父节点密钥



我正在尝试从快照中获取父节点的密钥。为了确认,当我打印snapshot.val()时,我从下面的函数中获得了正确的节点数据,所以问题不在于查询,但我似乎找不到获取返回快照密钥的方法。

/games
|--{game_id} ==> get this id
|---"alias":"123456" ==> from the snapshot returned by querying this
|---"players":...
|..... // other childs of game_id

以下是cloudfunction代码:

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) {
// tried snapshot.key => returns "games"
// tried snapshot.ref.key ==> returns "games"
return snapshot.ref.parent.key;
} else {
return "Unable to find game_id for the code";
}

}).catch(error => {
return error;
});

});

将代码更改为:

return gamesRef.orderByChild("alias").equalTo(code).limitToLast(1).once("value").then(snapshot => {
let gameId;
snapshot.forEach(child => {
gameId = child.key;
})
return gameId;
}).catch(error => {
return error;
});

相关内容

  • 没有找到相关文章

最新更新