如何在Firebase上随机获取相同的数据? [英] How can I randomly get same data on Firebase?

查看:58
本文介绍了如何在Firebase上随机获取相同的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户表和此代码.

I have user table and this code.

getOnline代码和连接的按钮

getOnline code and connected button

var onlineStatus = firebase.database().ref("users/" + firebase.auth().currentUser.uid + "/online");
onlineStatus.set(1);  

var dbUser = firebase.database();
var refUser = dbUser.ref("users");


refUser.orderByChild("online").equalTo(1).on("value", function(Data){
    console.log(Data.val(), Data.key);
});

我已经可以看到在线= 1个用户,但我想随机获得1个在线= 1的用户.

I can already see online=1 users, but I want to randomly get 1 user who has the online = 1.

我该怎么做?

更新

我想建立随机聊天.人们将根据标准进行匹配.就像Tinder App.想一想,有一个页面和按钮.当用户按下按钮时,它将与任何在线用户匹配.和聊天.

I want to build randomly chat. people will match according to the criteria. like Tinder App. Think about it, there is a page and button. When the user presses the button, it will match any online user. and chatting.

firebase用户;

firebase users;

users
     VIkvYAtLHxNqAwd722oKenriM7PJz2
        email: "mail@hotmail.com"
        online: 1 
        profile_picture:"https://scontent.xx.fbcdn.net" 
        username: "John Snow"

     DIkvYAtLHxNqAwd722oKenriM7PJz2
        email: "mail2@hotmail.com"
        online: 1 
        profile_picture:"https://scontent.xx.fbcdn.net" 
        username: "Jane Snow"

推荐答案

您可以使用数学. random()函数获取随机数(我们将其称为 n ),然后使第 n 位用户在线:

You can use the Math.random() function to get a random number (let's call it n) and then get the nth user online:

refUser.orderByChild("online").equalTo(1).on("value", function(Data){
    console.log(Data.val(), Data.key);
    var totalOnline = Data.numChildren(); //get number of online users
    var randomNr = Math.random() * totalOnline;
    var userIndex = parseInt(randomNr, 10); //parse the random number from double to integer
    var currentIndex = 0;
    Data.forEach(function(snap){
        if(currentIndex==userIndex){
            var randomUser = snap.val();
            //Do something with your random user
            break;
        }
        currentIndex++;
    });
});

还请注意,如果您有一个拥有成千上万用户的大型应用程序,则可能希望将查询限制为50个用户以提高应用程序的性能:

Also note that if you have a huge app with thousands of users, you might want to limit the query to 50 users to improve the app's performance:

refUser.orderByChild("online").equalTo(1).limitToFirst(50)

更新:要排除您自己的用户,您可以检查随机用户是否是您.如果是这样,请转到下一个:

Update: To exclude your own user you can check if the random user is you. And if it is, go to the next one:

        if(currentIndex>=userIndex && snap.key != firebase.auth().currentUser.uid){
            var randomUser = snap.val();
            //Do something with your random user
            break;
        }

更新2:我发现您不能在forEach循环上使用break.因此,您可以使用此处(使用BreakException)提供的解决方案来解决该问题:

Update 2: I've discovered that you can't use break on forEach loop. So you can solve that using the solution provided here (use a BreakException):

refUser.orderByChild("online").equalTo(1).on("value", function(Data){
    console.log(Data.val(), Data.key);
    var totalOnline = Data.numChildren(); //get number of online users
    var randomNr = Math.random() * totalOnline;
    var userIndex = parseInt(randomNr, 10); //parse the random number from double to integer
    var currentIndex = 0;
    var BreakException = {};
    try
    {
        Data.forEach(function(snap){
            if(currentIndex==userIndex){
                var randomUser = snap.val();
                //Do something with your random user
                throw BreakException;
            }
            currentIndex++;
        });
    }
    catch(e){
        if(e!== BreakException) throw e;
    }
});

这篇关于如何在Firebase上随机获取相同的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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