I have set up a database with Firebase and I have populated it with JSON information. I have code, taken directly from the Firebase documentation, that takes a snapshot of my data and updates it in real time. This is great for what I need, but I also need to access the values in my JSON to generate lists of content similarly to this:
var JSONStuff = json info on database
for (every array in the JSON)
{
//MAKE THINGS
}
I usually do this with php scripts and SQL databases, but I am using Firebase for the first time and I am not sure what I am doing wrong. Here is what I have so far.
JavaScript:
var display = document.getElementById("resultsDisplay");
var dbRef = firebase.database().ref().child("workouts");
// Sync with Firebase in real time.
dbRef.on("value", snap =>
{
//display.innerHTML = JSON.stringify(snap.val(), null, 3); This correctly prints my JSON.
JSON.stringify(snap.val(), null, 3);
});
JSON.parse(dbRef) //Trying to parse the JSON and do things with it.
{
for (var i = 0, len = dbRef.length; i < len; i++)
{
display.innerHTML = 'Routine: ' + dbRef.title + ' Exercises: ' + dbRef.name + '.';
}
};
HTML:
<div id="resultsDisplay">boo</div>
The console gives me the "Uncaught SyntaxError: Unexpected token h in JSON" error, which I know means there are double quotes messing with my parser somewhere, but as you can see I am not using them in the function.
I am a noob and therefore probably doing something completely silly, but any help would be greatly appreciated.
JSON.parsethe database reference, not the value. You need to operate inside the.on('value')callback and use thesnap.val()for your JSON, notdbRef.