Skip to content Skip to sidebar Skip to footer

How To Insert New Child Data In Firebase Web App?

I am building a simple car database app. When a user adds a new car it creates the below JSON data with blank fields for the gallery: { 'name': 'Cars', 'gallery': [{

Solution 1:

See this blog post on the many reasons not use arrays in our Firebase Database. Read it now, I'll wait here...

Welcome back. Now that you know why your current approach will lead to a painful time, let's have a look at how you should model this data. The blog post already told you about push IDs, which are Firebase's massively scalable, offline-ready approach to array-like collections.

As our documentation on reading and writing lists of data explains, you can add items to a list by calling push().

So if this creates a car:

function addStore(){
  var rootRef = firebase.database().ref();
  var storesRef = rootRef.child('app/cars');
  var newStoreRef = storesRef.push();
  newStoreRef.set({
    name: "Cars",
    "pageId": "23",
    "storeURL": "/app/cars/gallery"
  });
}

Then to add an image to the gallery of that store:

var newCarRef = newStoreRef.child('gallery').push();
newCarRef.set({
  title: 'Mercedes',
  img: 'http://'
})

This will add a new car/image to the end of the gallery (creating the gallery if it doesn't exist yet).

Solution 2:

If somebody wants it With Error Handling :

var rootRef = firebase.database().ref();
          var storesRef = rootRef.child('Lorelle/Visitors');
          var newStoreRef = storesRef.push();
          newStoreRef.set($scope.CarModal,
            function(error) {
              //NOTE: this completion has a bug, I need to fix.if (error) {
                   console.log("Data could not be saved." + error);
                   Materialize.toast('Error: Failed to Submit' + error, 2000);
              } else {
                 console.log("Data saved successfully.");
                 Materialize.toast('Data Submitted, Thank You.', 2000);

                   }
                });
             }

Post a Comment for "How To Insert New Child Data In Firebase Web App?"