My object from Firebase when I console.log:
Object {AW: Object, Qdoba: Object}
Then under the restaurant titles of AW and Qdoba, I have title and address which I can expand on and see in my console. I am wanting to display all the data from both restaurants in my webpage.
How do I access the two restaurant objects without knowing the AW and Qdoba? My code below.
// Initialize Firebase
var config = {
apiKey: "xxxxxxxx",
authDomain: "xxxxxxxx",
databaseURL: "xxxxxxxx",
storageBucket: "xxxxx.xxxxxx.com",
messagingSenderId: "xxxxx"
};
firebase.initializeApp(config);
var firebaseRef = firebase.database().ref('listings/');
//load
firebaseRef.on("value", function(snapshot) {
document
.querySelector('#contacts')
.innerHTML += contactHtmlFromObject(snapshot.val());
});
//prepare object's HTML
function contactHtmlFromObject(item){
console.log(item)
var html = '';
html += '<li class="list-group-item contact">';
html += '<div>';
html += '<p class="lead">'+item.title+'</p>';
html += '<p>'+item.title+'</p>';
html += '<p><small title="'
+item.title+'">'
+item.address
+'</small></p>';
html += '</div>';
html += '</li>';
return html;
}
My Firebase setup:
{
"listings" : {
"A&W" : {
"active" : true,
"address" : "3939",
"description" : "Super good root beer",
"title" : "A&W"
},
"Qdoba" : {
"active" : true,
"address" : "1234 main",
"description" : "A really good place to eat",
"title" : "Gellas"
}
}
}
for (var restaurant in object.listings)