正确的方式来存储值在Firebase中的数组 [英] Proper way to store values array-like in Firebase

查看:156
本文介绍了正确的方式来存储值在Firebase中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这首歌的名字,我正在为它储蓄收视率。
看起来像pic bellow,因为我使用 push()来存储每个新的评分。



<但是我想要的是什么?我想要的是什么?看到的是
> rating $ / code>作为
对象的数组,每个对象都包含这样的内容:

<$ p $ 投票人:{
ip:1.1.1.1,
stars:3,
}
$ b $因此 rating 键最终会是这样的:

 评分:[{ip:1.1.1.1,stars:3},...] 

然后,我可以启用每个IP地址只有一个投票。我在
Firebase 文档中找不到的是如何添加到现有的键,在这种情况下 rating (但它会是[list,like]) -
new hash 就像上面那样。
因为 push()自动创建 hash-key 值,我想追加
编辑:
我知道这个事实,Firebase本身不存储数组,这种情况并不重要,只是我想如何查看我的数据。

解决方案

您目前正在使用 push 来添加新的子项,这会自动为保证在多个客户端中唯一的新节点生成一个名称。

  ref.push({ip:userIP,stars:userStars}); 

Firebase的 push 方法是有多个客户添加项目到一个有序的集合。如果你想用数组完成同样的操作,你必须同步客户端之间的数组长度。 Firebase的名称生成不需要这样的开销,所以通常是在多个客户端保持订单收集的首选。



但是对于您的情况,您似乎并不想要一个有序的追加集合。看起来你认为IP地址是每个节点的标识。在这种情况下,我将使用IP地址作为节点名称的基础:

  votes 
1.1.1.1 :3
1.2.3.4:5
1.7.4.7:2

您可以通过以下方式来完成此操作:

  ref.child(userIP).set(userStars); 

如果您想随后读取数组中的所有选票,您可以执行:

  var votes = []; 
votesRef.on('value',function(snapshot){
snapshot.forEach(function(vote){
votes.push({ip:vote.key(),stars:vote .val()});
});
alert('There'+ votes.length +'votes');
})


I have a name of the song, with ratings I am saving for it. And it looks like pic bellow, as I am using push() to store each new rating.

But what I would like to see is, ratings as an array of objects, each containing something like this:

voter: {
    ip: "1.1.1.1",
    stars: 3,
}

So rating key would end up being something like this:

rating: [{ip: "1.1.1.1", stars: 3}, ...]

Then I can enable only one vote per IP address. What I couldn't find in Firebase docs, is how to add to an existing key, in this case rating(but it would be [list, like]) - a new hash like the one above. Because push() automatically creates hash-key value, and I want to append to an existing key.

EDIT: I am aware of the fact, that Firebase does not store arrays natively, however in this case it does not matter, it's just how I would like to see my data.

解决方案

You are currently using push to add new children, which automatically generates a name for the new node that is "guaranteed" to be unique across multiple clients.

ref.push({ ip: userIP, stars: userStars });

Firebase's push method is a great way to have multiple clients adding items to an ordered collection. If you want to accomplish the same using arrays, you'd have to synchronize the length of the array between the clients. Firebase's name generation doesn't require such overhead, so is normally the preferred of keeping an order collection across multiple clients.

But in your case, you don't seem to want an ordered "append only" collection. It seems like you consider the IP address to be an identification of each node. In that case I would use the IP address as the basis for the node name:

votes
  "1.1.1.1": 3
  "1.2.3.4": 5
  "1.7.4.7": 2

You would accomplish this with:

ref.child(userIP).set(userStars);

If you want to subsequently read all votes into an array, you can do:

var votes = [];
votesRef.on('value', function(snapshot) {
    snapshot.forEach(function(vote) {
        votes.push({ ip: vote.key(), stars: vote.val() });
    });
    alert('There are '+votes.length+' votes');
})

这篇关于正确的方式来存储值在Firebase中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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