Skip to content

Firebase database

Ryan Black edited this page Oct 16, 2018 · 1 revision

Firebase setup

Setting up a database in firebase is pretty self-explanatory, but after the internal setup is complete it's necessary to copy/paste the firebase import code to the bottom of any html page that you want firebase interactivity on. You paste it at the bottom of the html, but before any other scripts. (specifically, before any scripts where you're using firebase methods)

Interacting with a database from javaScript

Once you've imported firebase, you can store a reference to a part of your database (as setup from the firebase console) like so:

var ref = firebase.database().ref('internal_field_name');

From that variable, calling the following code:

ref.once('value').then(function(snapshot) { snapshot.val() }

snapshot.val() refers to the field specified in .ref(), in this case, a field called 'internal_field_name'. If that field had children, they can be referred to by that child's field name. for example, if internal_field_name had a child 'firstname' with a value of 'gumbo', you would 'get' the value 'gumbo' by the following code:

ref.once('value').then(function(snapshot) { var firstName = snapshot.val().firstname; }

At which point you can really start to make some magic, modifying and doing whatever to the data you've returned. When you want to do some 'setting' instead of 'getting', try this:

firebase.database().ref('internal_field_name').set({ firstname: bumbo });

It's easy and fun!

Clone this wiki locally