我想将数据保存到firebase多子节点:
--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(包括所有子元素)
目前我得到:无法阅读财产' participant_id'未定义的 在Object.factory.postEvents 但我拥有数据数组中的所有值。
答案 0 :(得分:7)
您可以简单地将整个事件推送到Firebase。
firebaseRef.child("events").push(data); // bad practice, don't do this!
但这意味着你最终会在服务器上为子节点使用数组索引。众所周知,数组索引可能会导致分布式数据结构出现问题。因此,只要您拥有类似列表的结构,最好只将push
每个项目放入Firebase,然后让它们为您生成唯一的ID。
对于您的情况,这意味着您需要循环并推送每个event
(示例中只有一个),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
});
});
});