scrimba
Frontend Career Path
Making websites interactive
Build a Mobile App
For loop to render database items
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

For loop to render database items
AboutCommentsNotes
For loop to render database items
Expand for more info
index.js
run
preview
console
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js"
import { getDatabase, ref, push, onValue } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js"

const appSettings = {
databaseURL: "https://realtime-database-df319-default-rtdb.europe-west1.firebasedatabase.app/"
}

const app = initializeApp(appSettings)
const database = getDatabase(app)
const shoppingListInDB = ref(database, "shoppingList")

const inputFieldEl = document.getElementById("input-field")
const addButtonEl = document.getElementById("add-button")
const shoppingListEl = document.getElementById("shopping-list")

addButtonEl.addEventListener("click", function() {
let inputValue = inputFieldEl.value

push(shoppingListInDB, inputValue)

clearInputFieldEl()

appendItemToShoppingListEl(inputValue)
})

onValue(shoppingListInDB, function(snapshot) {
let itemsArray = Object.values(snapshot.val())

// Challenge: Write a for loop to iterate on itemsArray and console log each item
})

function clearInputFieldEl() {
inputFieldEl.value = ""
}

function appendItemToShoppingListEl(itemValue) {
shoppingListEl.innerHTML += `<li>${itemValue}</li>`
}
Console
[
"Oranges"
,
"Chocolate"
,
"Red bull"
,
"Bread"
,
"Bread"
]
,
/index.html
-2:57