如何平均对多个Firebase子节点进行投票? [英] How to average votes over a number of Firebase child nodes?
问题描述
这个问题已经被问过了,但我不太明白答案。 / 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.
- Retrieve all the data
- Determine how many entries there are in that same data
- 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 bothvalue
andchild_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屋!