I am trying to figure out how to save data to a Firebase multi-child node structure:
--Events
----Races
-------Participants
Below is some dummy data example that represents the type of data I need to store in 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;
});
I'm struggling with why the push method isn't saving the entire array of data and pushing it to Firebase, along with all its child elements.
Currently, I am encountering the error message: Cannot read property 'participants_id' of undefined at Object.factory.postEvents However, all values are present in the data array.