如何平均对多个Firebase子节点进行投票? [英] How to average votes over a number of Firebase child nodes?

查看:125
本文介绍了如何平均对多个Firebase子节点进行投票?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问过了,但我不太明白答案。 / p>

我需要计算来自Firebase的数据数组的平均值,所以我有两个步骤。 b $ b

  • 检索所有数据

  • 确定同一数据中有多少条目

  • 计算平均值



  • 我的代码:

    $ p $ myDataRef.orderByChild( (child_added,function(snapshot){
    votes = snapshot.val()。vote;
    //console.log (投票值:+票)
    });
    $ b myDataRef.orderByChild(time)。startAt(time-180000).endAt(time).on(value,function(snapshot){
    numberOfVotes = snapshot.numChildren );
    //console.log(Number of Votes:+ numberOfVotes)
    });

    函数calculateAverage(numberOfVotes,votes){
    return eval(votes.join('+'))/ numberOfVotes;


    console.log(calculateAverage)

    我错误地解释了一些超级基本的东西,因为我无法弄清楚如何将Firebase查询中的数据取出并转换为函数。我缺少什么?

    解决方案

    Firebase异步加载数据。你只能确定你的平均数据查询完成加载完全。

    一个想到的方法是:

      myDataRef.orderByChild(time)。startAt(time-180000).endAt(time).on(value,function ){
    var voteCount = snapshot.numChildren();
    var total = 0;
    snapshot.forEach(function(voteSnapshot){
    total + = voteSnapshot.val()。投票;
    ));
    console.log(Average =+ total / voteCount);
    });

    有些注意事项:


    • 我使用一个事件。不知道为什么你同时使用 child_added ,但它似乎只是复杂的事情。 b $ b


    也可以在收听小孩_ 事件时保持运行的平均值,但是您会必须倾听所有人的意见。

      var total = 0,
    voteCount = 0;
    query = myDataRef.orderByChild(time)。startAt(time-180000).endAt(time);
    query.on(child_added,function(snapshot){
    voteCount + = 1;
    total + = snapshot.val()。vote;
    console.log(运行平均值:+ total / voteCount);
    });
    query.on(child_removed,function(snapshot){
    voteCount - = 1;
    total - = snapshot.val()。vote;
    console.log(运行平均值:+ total / voteCount);
    });


    This question has been asked before but I didn't quite understand the answer.

    I need to calculate the average of an array of data from Firebase, so I have two steps.

    1. Retrieve all the data
    2. Determine how many entries there are in that same data
    3. Calculate the average

    My code:

    myDataRef.orderByChild("time").startAt(time-180000).endAt(time).on("child_added", function(snapshot) {
        votes = snapshot.val().vote;
        //console.log("Vote value:" + votes)
    });
    
    myDataRef.orderByChild("time").startAt(time-180000).endAt(time).on("value", function(snapshot) {
        numberOfVotes = snapshot.numChildren();
        //console.log("Number of Votes:" + numberOfVotes)
    });
    
    function calculateAverage(numberOfVotes, votes) {
        return eval(votes.join('+')) / numberOfVotes;
    }
    
    console.log(calculateAverage)
    

    I think I'm misinterpreting something super basic because I can't figure out how to get the data "out" of the Firebase query and into a function. What am I missing?

    解决方案

    Firebase loads data asynchronously. You can only determine you average once the data the query has been loaded completely.

    One way that comes to mind is:

    myDataRef.orderByChild("time").startAt(time-180000).endAt(time).on("value", function(snapshot) {
        var voteCount = snapshot.numChildren();
        var total = 0;
        snapshot.forEach(function(voteSnapshot) {
            total += voteSnapshot.val().vote;
        });
        console.log("Average=" + total / voteCount);
    });
    

    Some things of note:

    • I use a single value event. Not sure why you were using both value and child_added, but it only seemed to complicate things.

    It is also possible to keep a running average while listening to child_ events, but you'll have to listen to all of them.

    var total = 0, 
        voteCount = 0;
        query = myDataRef.orderByChild("time").startAt(time-180000).endAt(time);
    query.on("child_added", function(snapshot) {
        voteCount += 1;
        total += snapshot.val().vote;
        console.log("Running average: "+total/voteCount);
    });
    query.on("child_removed", function(snapshot) {
        voteCount -= 1;
        total -= snapshot.val().vote;
        console.log("Running average: "+total/voteCount);
    });
    

    这篇关于如何平均对多个Firebase子节点进行投票?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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