After setting up a Firebase database and populating it with JSON data, I found code from the Firebase documentation that allows me to update the data in real time. While this works well for my needs, I also want to access the values in the JSON to create lists of content like this:
var JSONStuff = json info on database
for (every array in the JSON)
{
//GENERATE CONTENT
}
My usual approach involves PHP scripts and SQL databases, but experimenting with Firebase for the first time has left me feeling uncertain about what I might be doing incorrectly. Here is my current progress:
JavaScript:
var display = document.getElementById("resultsDisplay");
var dbRef = firebase.database().ref().child("workouts");
// Real-time sync with Firebase.
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) //Attempting to parse the JSON and perform operations on 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 displays an error "Uncaught SyntaxError: Unexpected token h in JSON," indicating that there may be quotation marks causing issues with the parser, even though none are used in the function.
As a beginner in this area, I may be overlooking something trivial, so any assistance would be greatly valued.