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

391
Visualizações
What is the equivalent of CollectionReference.doc() for version 9 of the Firebase JS SDK?

I am trying to add a new document in a firestore transaction with version 9 of the Firebase JS SDK. In version 8, we can use

const newDocRef = db.collection('coll').doc();

to get a DocumentReference to a document that does not exist yet, and then do a

transaction.set(newDocRef, { ... });

to set the document in a transaction. How would we do the same in version 9? The new CollectionReference class does not seem have a doc() method as in version 8. Thanks!

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

0

There's a function doc() to create a DocumentReference and then runTransaction() to run a transaction. Try refactoring the code like this:

import { getFirestore, doc, runTransaction } from "firebase/firestore"

const newDocRef = doc(db, "col_name", "doc_id");

await runTransaction(db, async (transaction) => {
  // ...
  transaction.set(newDocRef, { ...data });
});

The documentation has examples with both the name-spaced and functional syntax. Also checkout this answer for better explanation of syntax differences in V8 and V9:

Firestore: What's the pattern for adding new data in Web v9?

about 4 years ago · Juan Pablo Isaza Relatório

0

Found the answer here. This works for me:

const newDocRef = doc(collection(db, "coll"));

transaction.set(newDocRef, { ... });
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