Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
Why refreshing data duplicates dom elements?

I've made this library app where the user can post a book in their account. When the user post a new Book it's supposed to refresh data, fetching books, including the newest and display them, that works. The problem is that when it does the fetch and display all books again, it duplicates the dom elements instead of overwriting them. Here's the code:

//POST THE NEW BOOK
addBookForm.addEventListener('submit', (e) => {
    e.preventDefault()
    const title = document.querySelector('#bookTitle').value
    const author = document.querySelector('#bookAuthor').value
    const pages = document.querySelector('#bookPages').value
    const description = document.querySelector('#bookDescription').value
    const image = document.querySelector('#bookImage').files[0]
    const read = document.querySelector('#bookRead').checked
    
    addBookForm.reset()
    addBookModal.hide()
    
    uploadToUserRef(image, title)
    .then(image =>{
        addNewBook(title, author, pages, description, image, read)
    })
    .then(fetchBooks)
    .then(books => {
        books.forEach(book => {
            addCardToHTML(book.data().title, book.data().author, book.data().pages, book.data().description, book.data().image, book.data().read)
        })
    })
    

})

// DISPLAY BOOKS
function fetchBooks() {
    const docRef = db.collection('users').doc(auth.currentUser.uid).collection('books')
    return new Promise((resolve, reject) => {
        docRef.get().then(querySnapshot => {
            resolve(querySnapshot.docs)
        }).catch(err => {
            console.log(err);

        })

    })
}

// INJECT THE BOOK CARDS INTO THE HTML
function addCardToHTML(title, author, pages, description, image, read) {

    const card = `<div class="card shadow col-sm-12 col-lg-3 m-3" style="max-width: 15rem; min-height: 350px;">
    <div class="card-body">
      <div class="mb-2" style="background-position: center; background-repeat: no-repeat; background-size: cover; background-image: url(${image}); height: 150px; width: 190px;" ></div>
      <h5 class="card-title text-danger">${title}</h5>
      <h6 class="card-subtitle mb-2 text-muted">${author} ${pages}</h6>
      <p class="card-text">${description}</p>
      <span class = ${read == true ? "text-success": "text-danger"}>${read == true ? "I've read this book" : "I haven't read this book yet"}</span>
    </div>
  </div>`

    library.innerHTML += card
}

I've tried to clear the feed before displaying the book cards again, but it just clear the feed and didn't display any card. Thanks!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda