Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda