get data from firebase collection, query or document
get collection or query data
getDocs(booksRef)
.then((snapshot) => {
// console.log(snapshot.docs)
let books = []
snapshot.docs.forEach((doc) => {
books.push({ ...doc.data(), id: doc.id })
})
console.log(books)
})
.catch(err => {
console.log(err.message)
})get doc data
getDoc(bookRef)
.then( doc => {
console.log(doc.data(), doc.id)
})add doc with generated key locally
firebase document can hold up to 1MB
const addBookForm = document.querySelector(".add")
addBookForm.addEventListener('submit', e => {
e.preventDefault();
// `booksRef` is query or collection
addDoc(booksRef, {
title: addBookForm.title.value,
author: addBookForm.author.value,
createdAt: serverTimestamp() // don't trust local dates
})
.then(() => {
addBookForm.reset()
})
.catch((err) => {
console.error(err.message)
})
})