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

117
Visualizações
Get a DocumentReference without providing an ID in Firebase 9 web version Javascript SDK

I'm trying to commit a batch write using firebase 9 modular sdk. This operation includes creating a post document, ideally with an auto generated id.

const batch = writeBatch(db)
const postRef = doc(db, 'posts') // this fails, must pass and id
const threadRef = doc(db, 'threads', post.threadId)
batch.set(postRef, post)
batch.update(threadRef,
    'posts', arrayUnion(postRef.id),
    'contributors', arrayUnion(state.authId))
await batch.commit()

In Firebase 8 it's possible to obtain a document reference with a random generated id using with the following code.

firebase.firestore().collection(nameOfTheCollection).doc()

I'm trying to accomplish the same thing using Firebase 9 modular sdk, but I can't find any method to do this. Is it possible to get a document reference with a random generated id without creating the document first with Firebase 9?

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

0

You can use the doc() function and pass a CollectionReference as first parameter without any path segments as shown below:

import { doc, collection } from "firebase/firestore";

const docRef = doc(collection(db, "colName")); // Don't specify an ID yourself

console.log(`New Document ID: ${docRef.id}`);
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