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

302
Vistas
How to create a custom id document in firestore(Firebase modular SDK (v9))

How can we create a document with a custom id in firestore with the new modular SDK V9?

const register = async function (adminEmail){
  try{
    const docSnap = await getDoc(availableIdRef);
    const collegeId = docSnap.data().id; //This should be the document id

    const adminRef = collection(firestore, 'admin');
    await addDoc(adminRef, {adminEmail, collegeId});

  }catch(e){
    alert("Error!");
  }
}

I want to create a new document with id as collegeId in admin collection.

I know how to do this in web version 8(namespaced) but have no idea about web version 9(modular).

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use setDoc() method to specify an ID instead of addDoc which will generates a random ID as mentioned in the documentation:

const adminRef = doc(firestore, 'admin', collegeId);
//       doc  --->^
await setDoc(adminRef, {email: adminEmail, id: collegeId}) // overwrites the doc if it already exists
//    ^^^

Also note that to create a DocumentReference you need to use doc() and not collection() which is used to create a CollectionReference. I've explained difference in these 2 here: Firestore: What's the pattern for adding new data in Web v9?

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