最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase: push multi child data using function - Stack Overflow

programmeradmin2浏览0评论

I want to save data to firebase multi child node:

--Events
----Races
-------Participants

Here is a dummy data, This a type of data that I want to save to 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("/");

        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;
  });

My Question is: Why push is not storing data array and pushing it to Firebase(including all child elements)

currently I am getting: Cannot read property 'participants_id' of undefined at Object.factory.postEvents But I have all the values in data array.

I want to save data to firebase multi child node:

--Events
----Races
-------Participants

Here is a dummy data, This a type of data that I want to save to 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./");

        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;
  });

My Question is: Why push is not storing data array and pushing it to Firebase(including all child elements)

currently I am getting: Cannot read property 'participants_id' of undefined at Object.factory.postEvents But I have all the values in data array.

Share Improve this question edited Aug 25, 2014 at 5:59 Dixit asked Aug 25, 2014 at 5:42 DixitDixit 13.1k4 gold badges51 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You could simply push the entire event to Firebase in one big push.

firebaseRef.child("events").push(data); // bad practice, don't do this!

But that means you'll end up using array indices for child nodes on the server. Array indices are notoriously likely to cause problems in distributed data structures. So whenever you have a list-like structure, it is best to simply push each item into Firebase and let them generate a unique ID for you.

For your case that means that you need to loop-and-push for each event (only one in the example), race and participant.

Code sample:

    // 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

            });
        });
    });
发布评论

评论列表(0)

  1. 暂无评论