我想获取我的Firebase数据的平均值 [英] I want to get the average my firebase data

查看:30
本文介绍了我想获取我的Firebase数据的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在FireBase中的数据更新时平均相关值.

I want to average the related values ​​when the data in the FireBase is updated.

我正在使用Firebase函数,无法加载数据.事件发生时我可以更改所需的数据,但无法计算数据的平均值.

I am using Firebase functions and can not load data. I can change the data I want when the event occurs, but I can not calculate the average of the data.

exports.taverage = functions.database.ref('/User/tsetUser/monthQuit/{pushId}')
    .onCreate((snapshot, context) => {
        const promiseRoomUserList = admin.database().ref('/User/tsetUser/monthQuit/{pushId}').once('value');
        var sum=0;
        const arrayTime = [];
            snapshot.forEach(snapshot => {
               arrayTime.push('/User/tsetUser/monthQuit/{pushId}'.val());
            })
        for(let i=0; i<arrayTime.length; i++){
            sum+=arrayTime[i];
        }
        return admin.database().ref('/User/tsetUser/inform/standardQuit').set(sum);
    });

//我要设置"standardQuit"值的平均值.

//I Want 'standardQuit' value set average.

推荐答案

我不确定为什么无法计算平均值,但是代码的简单版本是:

I'm not sure why you can't calculate the average, but a simpler version of your code would be:

exports.taverage = functions.database.ref('/User/tsetUser/monthQuit/{pushId}')
.onCreate((snapshot, context) => {
    return admin.database().ref('/User/tsetUser/monthQuit/{pushId}').once('value')
    .then(function(snapshot) {
        let sum=0;
        snapshot.forEach(child => {
            sum = sum + child.val();
        })
        let avg = sum / snapshot.numChildren();

        return admin.database().ref('/User/tsetUser/inform/standardQuit').set(avg);
    });
});

最大的区别:

  • 此代码从顶层和嵌套的 then()返回promise.这是必需的,因此Cloud Functions会知道您的代码何时完成,从而可以停止向您收费(并有可能关闭容器).

  • This code returns promises from both the top-level, and the nested then(). This is needed so Cloud Functions knows when your code is done, and it can thus stop billing you (and potentially shut down the container).

我们只是将每个子代的值添加到总和中,因为您没有以其他任何方式使用数组.请注意, child.val()取决于您的数据结构,您没有共享它.因此,如果该操作失败,则需要更新获取确切值的方式(或与我们共享数据结构).

We simply add the value of each child to the sum, since you weren't using the array in any other way. Note that the child.val() depends on your data structure, which you didn't share. So if it fails there, you'll need to update how you get the exact value (or share you data structure with us).

该代码实际上通过将总和除以子节点数来计算平均值.

The code actually calculates the average by dividing the sum by the number of child nodes.

要记住的一件事是,每次添加一个节点时,您现在都在读取所有节点.随着节点的添加,此操作将变得越来越昂贵.考虑是否可以使用移动平均线,该移动平均线不需要所有子节点,而仅需要当前平均线和新的子节点.该值将是一个近似平均值,而最近的值通常具有更大的权重,并且计算起来便宜得多:

One thing to keep in mind is that you're now reading all nodes every time one node gets added. This operation will get more and more expensive as nodes are added. Consider if you can use a moving average, which wouldn't require all child nodes, but merely the current average and the new child node. The value will be an approximate average where more recent value typically have more weight, and is much cheaper to calculate:

exports.taverage = functions.database.ref('/User/tsetUser/monthQuit/{pushId}')
.onCreate((snapshot, context) => {
  return admin.database().ref('/User/tsetUser/inform/standardQuit').transaction(function(avg) {
    if (!avg) avg = 0;
    return (15.0 * avg + snapshot.val()) / 16.0;
  });
});

这篇关于我想获取我的Firebase数据的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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