Javascript 数字顺序ID';Firebase中数据对象的分类
我对“游戏”还很陌生,我想知道是否有可能将新添加的数据(通过表单和输入)以数字形式订购到Firebase,以便每个新数据条目都获得ID(最后添加的数据的编号+1)。 为了更清楚,您可以在下面找到当前添加数据的方式的屏幕截图。数据点0-7是现有的(JSON导入的数据),具有随机创建的ID的数据点属于新条目。我希望条目符合Firebase内部的编号,否则我的D3条形图将无法可视化Javascript 数字顺序ID';Firebase中数据对象的分类,javascript,json,forms,input,firebase,Javascript,Json,Forms,Input,Firebase,我对“游戏”还很陌生,我想知道是否有可能将新添加的数据(通过表单和输入)以数字形式订购到Firebase,以便每个新数据条目都获得ID(最后添加的数据的编号+1)。 为了更清楚,您可以在下面找到当前添加数据的方式的屏幕截图。数据点0-7是现有的(JSON导入的数据),具有随机创建的ID的数据点属于新条目。我希望条目符合Firebase内部的编号,否则我的D3条形图将无法可视化 var firebaseData = new Firebase("https://assignment5.firebas
var firebaseData = new Firebase("https://assignment5.firebaseio.com");
function funct1(evt)
{
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
firebaseData.push().set({games: gameName, medals: medalCount, summergames: bool});
evt.preventDefault();
}
var submit = document.getElementsByTagName('button')[0];
submit.onclick = funct1;
更新:
function funct1(evt)
{
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
for (var i = 0; i < 10; i++)
{
函数funct1(evt)
{
var gameName=$('#nameInput').val();
var medalCount=$(“#medalsInput”).val();
var bool=$('#boolInput')。是(':checked');
对于(变量i=0;i<10;i++)
{
firebaseData.child('7'+i).set({游戏:游戏名称,奖牌:medalCount,夏季游戏:bool}(i));}
问题:
有两种方法可以为文档节点生成ID
.push()
,将生成该唯一id.set()
将允许您使用自己的
身份证.push().set({})
,因此push将生成一个新的id,而set只需设置数据
// These two methods are equivalent
listRef.push().set({user_id: 'wilma', text: 'Hello'});
listRef.push({user_id: 'wilma', text: 'Hello'});
使用.set()
而不使用.push()
将允许您控制自己的id
使用.push()
:
在Firebase中管理数据列表时,使用唯一生成的ID非常重要,因为数据正在实时更新。如果使用整数ID,数据很容易被覆盖
仅仅因为你有一个唯一的id,并不意味着你不能用你的id查询你的数据。你可以循环一个父引用,也可以得到所有的子引用
var listRef = new Firebase('https://YOUR-FIREBASE.firebaseio.com/items');
// constructor for item
function Item(id) {
this.id = id;
};
// add the items to firebase
for (var i = 0; i < 10; i++) {
listRef.push(new Item(i));
};
// This will generate the following structure
// - items
// - LGAJlkejagae
// - id: 0
// now we can loop through all of the items
listRef.once('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var name = childSnapshot.name();
var childData = childSnapshot.val();
console.log(name); // unique id
console.log(childData); // actual data
console.log(childData.id); // this is the id you're looking for
});
});
要获取数据,可以使用上面相同的方法循环节点中的所有子项
那么使用哪一个呢?
如果不希望数据容易被覆盖,请使用.push()
当您的id对您非常非常重要,并且您不关心数据是否容易被覆盖时,请使用.set()
编辑
您遇到的问题是,您需要知道列表中的项目总数。因此,您需要加载数据并获取项目数。如果您确实希望维护该id结构,我建议在页面加载和缓存时执行此操作。这将导致性能问题
然而,如果你知道你需要索引什么,或者不想覆盖你的索引,我不会从firebase加载数据
在您的情况下,您的代码如下所示:
// this variable will store all your data, try to not put it in global scope
var firebaseData = new Firebase('your-firebase-url/data');
var allData = null;
// if you don't need to load the data then just use this variable to increment
var allDataCount = 0;
// be wary since this is an async call, it may not be available for your
// function below. Look into using a deferred instead.
firebaseData.once('value', function(snapshot) {
allData = snapshot.val();
allDataCount = snapshot.numChildren(); // this is the index to increment off of
});
// assuming this is some click event that adds the data it should
function funct1(evt) {
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
firebaseData.child('/' + allDataCount).set({
games: gameName,
medals: medalCount,
summergames: bool
});
allDataCount += 1; // increment since we still don't have the reference
};
有关在Firebase中管理列表的更多信息,Firebase API文档中有一篇很好的文章。你能提供一些代码吗?嗨,大卫,这是我目前收集数据的方式:var firebaseData=new Firebase(“);function funct1(evt){var gameName=$('#nameInput')。val();var medalCount=$(“#medalsInput”).val();var bool=$(“#boolInput”).is(':选中“);firebaseData.push().set({games:gameName,奖牌:medalCount,summergames:bool});evt.preventDefault();}var submit=document.getElementsByTagName('button')[0];submit.onclick=funct1;你好,大卫,谢谢你的建议和信息。我想使用.set()并覆盖数据在我的情况下不是什么大问题。我重构了一段代码如下:函数funct1(evt){var gameName=$('#nameInput').val();var medalCount=$('#medalsInput').val();var bool=$(“#boolInput”).is(':checked');for(var i=0;i<10;i++){firebaseData.child('7'+i).set({游戏:游戏名称,奖牌:医疗帐户,夏季游戏:bool}(i));};但这似乎不起作用。我希望新数据从8开始计算。Tnx!你能在问题中更新它而不是在评论中吗?谢谢你的耐心,我已经更新了问题区域的一段代码。我希望问题的解决方案现在会更清楚。谢谢你的回答!它确实帮了我很多。这太傻了在d3可视化代码无法抓取随机创建/编号的Firebase数据ID后面的数据。
// this variable will store all your data, try to not put it in global scope
var firebaseData = new Firebase('your-firebase-url/data');
var allData = null;
// if you don't need to load the data then just use this variable to increment
var allDataCount = 0;
// be wary since this is an async call, it may not be available for your
// function below. Look into using a deferred instead.
firebaseData.once('value', function(snapshot) {
allData = snapshot.val();
allDataCount = snapshot.numChildren(); // this is the index to increment off of
});
// assuming this is some click event that adds the data it should
function funct1(evt) {
var gameName = $('#nameInput').val();
var medalCount = $('#medalsInput').val();
var bool = $('#boolInput').is(':checked');
firebaseData.child('/' + allDataCount).set({
games: gameName,
medals: medalCount,
summergames: bool
});
allDataCount += 1; // increment since we still don't have the reference
};