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

384
Visualizações
Firebase addDoc not writing data to Firestore

I'm trying to write some data to my Firestore database. I've been following Firebase docs but for some reason nothing is being written to my database. I'm not receiving any console errors but there are no new docs being written. I've also tried using setDoc, but no luck with that either.

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.8/firebase-app.js";
import { getFirestore, doc, setDoc, addDoc, collection } from "https://www.gstatic.com/firebasejs/9.6.8/firebase-firestore.js";

const firebaseConfig = {
   ...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore();

var submitPost = document.getElementById("submitPost");

submitPost.addEventListener("click", writeToDB, false);

async function writeToDB() {
   console.log("HIT ME")
   const newDoc = await addDoc(collection(db, 'posts'), {
       test: 'TEST',
       please: 'WORK',
   });
};

Here is my Firestore console after running the function, it does not contain any new documents, only one I have already made manually: enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

when you use async/await syntax instead .then() make sure to put a try/catch around your await to capture any possible error in your Promise.

  async function writeToDB() {
    console.log("HIT ME")
    try {
      const newDoc = await addDoc(collection(db, 'posts'), {
        test: 'TEST',
        please: 'WORK', });

    } catch(err) {
      console.error("writeToDB failed. reason :", err)
    }
  };

if you don't enclose it in try/catch any promise fail will be silent. And it would be equivalent of not having .catch() after .then()

about 4 years ago · Juan Pablo Isaza Relatório
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