Javascript Firebase:使用函数推送多个子数据
我想将数据保存到firebase多子节点:Javascript Firebase:使用函数推送多个子数据,javascript,angularjs,firebase,angularfire,Javascript,Angularjs,Firebase,Angularfire,我想将数据保存到firebase多子节点: --Events ----Races -------Participants 这是一个虚拟数据,这是我想保存到firebase的数据类型 var dummyData = [ { event_id: 1, event_venue: "Churchill Ground Venue", event_name: "Victoria C
--Events
----Races
-------Participants
这是一个虚拟数据,这是我想保存到firebase的数据类型
var dummyData = [
{
event_id: 1,
event_venue: "Churchill Ground Venue",
event_name: "Victoria Cup",
event_type: "Holiday Special",
event_datetime: now,
event_url: 'www',
races: {
1: {
race_id: 1,
race_type: "Horse Race",
race_url: "www",
race_name: "Super Hourse 8",
race_venue: "Ground No 7",
race_datetime: now,
races_participants: {
1: {
participants_id: 1211,
participants_name: "Doll Fire",
participants_place_odds: 10,
participants_win_odds: 5,
participants_result: 0,
}
}
},
2: {
race_id: 2,
race_type: "Horse Race 2",
race_url: "www",
race_name: "Super Hourse 9",
race_venue: "Ground No 7",
race_datetime: now,
races_participants: {
participants_id: {
participants_id: 222,
participants_name: "Doll Fire 2",
participants_place_odds: 130,
participants_win_odds: 54,
participants_result: 03,
}
}
}
},
},
];
//calling Services to post data
EventsFactory.postEvents(dummyData[0]);
myApp.factory('EventsFactory', function(){
var factiry = {};
//post data to fb server
factory.postEvents = function (data) {
//passed data must be object
var firebaseRef = new Firebase("https://api.firebaseio.com/");
firebaseRef.child("events").push({
event_id: data.event_id,
event_venue: data.event_venue,
event_name: data.event_name,
event_type: data.event_type,
event_url: data.event_url,
event_datetime: data.event_datetime,
races: {
//not sure how to store multiple child, Cant call for each to loop
//each paased object from data var
race_id: {
race_id: data.races.race_id,
race_type: data.races.race_type,
race_url: data.races.race_url,
race_name: data.races.race_name,
race_venue: data.races.race_venue,
race_datetime: data.races.race_datetime,
races_participants: {
participants_id: {
participants_id: data.races.races_participants.participants_id,
participants_name: data.races.races_participants.participants_name,
participants_place_odds: data.races.races_participants.participants_place_odds,
participants_win_odds: data.races.races_participants.participants_win_odds,
participants_result: data.races.races_participants.participants_result
}
}
}
},
});
}
return factory;
});
我的问题是:为什么push不存储数据数组并将其推送到firebase,包括所有子元素
当前我得到:无法读取未定义的属性“参与者\u id”
在Object.factory.postEvents
但我有数据数组中的所有值 您只需一次大推就可以将整个事件推送到Firebase
firebaseRef.child("events").push(data); // bad practice, don't do this!
但这意味着您将在服务器上使用子节点的数组索引。众所周知,数组索引可能会在分布式数据结构中引起问题。因此,每当您有一个类似列表的结构时,最好只需将每个项目推送到Firebase中,让它们为您生成一个唯一的ID
对于您的情况,这意味着您只需要为每个事件循环和推动示例中的一个事件,即race和participant
代码示例:
// Add the event
var event = firebaseRef.child("events").push({
event_id: data.event_id,
event_venue: data.event_venue,
event_name: data.event_name,
event_type: data.event_type,
event_url: data.event_url,
event_datetime: data.event_datetime
});
// Add then races to the event
data.races.forEach(function(race) {
var race = event.child("races").push({
race_id: race.race_id,
race_type: race.race_type,
race_url: race.race_url,
race_name: race.race_name,
race_venue: race.race_venue,
race_datetime: race.race_datetime,
});
// Add the participants to the race
race.race_participants.forEach(function(participant) {
race.child("participants").push({
participants_id: participant.participants_id,
participants_name: participant.participants_name,
participants_place_odds: participant.participants_place_odds,
participants_win_odds: participant.participants_win_odds,
participants_result: participant.participants_result
});
});
});
你可以简单地把整个事件推到Firebase上
firebaseRef.child("events").push(data); // bad practice, don't do this!
但这意味着您将在服务器上使用子节点的数组索引。众所周知,数组索引可能会在分布式数据结构中引起问题。因此,每当您有一个类似列表的结构时,最好只需将每个项目推送到Firebase中,让它们为您生成一个唯一的ID
对于您的情况,这意味着您只需要为每个事件循环和推动示例中的一个事件,即race和participant
代码示例:
// Add the event
var event = firebaseRef.child("events").push({
event_id: data.event_id,
event_venue: data.event_venue,
event_name: data.event_name,
event_type: data.event_type,
event_url: data.event_url,
event_datetime: data.event_datetime
});
// Add then races to the event
data.races.forEach(function(race) {
var race = event.child("races").push({
race_id: race.race_id,
race_type: race.race_type,
race_url: race.race_url,
race_name: race.race_name,
race_venue: race.race_venue,
race_datetime: race.race_datetime,
});
// Add the participants to the race
race.race_participants.forEach(function(participant) {
race.child("participants").push({
participants_id: participant.participants_id,
participants_name: participant.participants_name,
participants_place_odds: participant.participants_place_odds,
participants_win_odds: participant.participants_win_odds,
participants_result: participant.participants_result
});
});
});
这不是一个糟糕的设计吗?因为如果您只想获取基本事件信息,那么最终将获取嵌套在父节点下的所有内容。并不是说你的问题没有回答这个人最初的询问,但当任何特定节点下的项目数量可能变得很大时,这有点违反了结构化数据的模式。你最终可能会检索到5000名参与者,只是为了获得有关事件/比赛的基本信息。这是一篇讨论其中一些概念的好文章。这不是一个糟糕的设计吗?因为如果您只想获取基本事件信息,那么最终将获取嵌套在父节点下的所有内容。并不是说你的问题没有回答这个人最初的询问,但当任何特定节点下的项目数量可能变得很大时,这有点违反了结构化数据的模式。你最终可能会检索到5000名参与者,只是为了获得有关事件/比赛的基本信息。这是一篇讨论其中一些概念的好文章。