如何获取项目在实时数据库中的位置 [英] How to get the position of an item in the realtime database

查看:41
本文介绍了如何获取项目在实时数据库中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取项目在数据库中的位置以及数据.

I want to get the position of an item in the database, along with the data.

.ref("users")
.orderByChild("email")
.equalTo(myEmail)
.once("value", snapshot => {
    console.log("Database Position", ???? )
}

快照中是否可以使用一个值指示数据库中的位置(通过电子邮件订购后)?我不想在客户端执行此操作,因为我有超过1000个用户

Is there a value in the snapshot that I could use that indicates the position in the database (after ordering by email)? I don't want to do this on the client side since I have >1k users

推荐答案

Firebase实时数据库不会跟踪节点在其父节点中的位置,也不会在查询结果中返回此类位置.这意味着确定一个子节点在一组子节点中的位置的唯一方法是,在要查找的子节点之前检索所有子节点.

The Firebase Realtime Database does not track the position of nodes in their parents, nor does it return such a position in query results. This means the only way to determine the position of a child within a set of child nodes, is to retrieve all child nodes before the one you're looking for.

.ref("users")
.orderByChild("email")
.endAt(myEmail)
.once("value", snapshot => {
    var index = 0;
    snapshot.forEach(function(childNode) {
        if (childNode.child("email").val() === myEmail) {
            console.log("Database Position", index);
        }
        else {
            index = index + 1;
        }
    });
}

由于您评论您正在尝试创建排行榜,请参阅:

Since you commented that you're trying to create a leaderboard, see:

这篇关于如何获取项目在实时数据库中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆