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

158
Vistas
How to get auto-generated ID of newly created document in a subcollection in Firestore Web V9? Javascript/React

onClick I create a new document within a subcollection with a user's document.

Here's that function:

// Create a new checkout session in the subcollection inside this users document

const createCheckoutSession = async () => {
const collectionRef = collection(db, 'users', currentUser.uid, 'checkout_sessions');
const payload = {
  price: 'price_1JvjWLChfN4TVWbYw2LdDXZZ',
  success_url: window.location.origin,
  cancel_url: window.location.origin
};

await addDoc(collectionRef, payload);

// TODO – Get the newly created document's id. 

};

My goal is to get the newly created document's ID. To then get a field within that document to fire off a redirect to Stripe checkout.

In Firestore Web V8, I'm pretty certain I would use:

collectionRef.onSnapshot(async (snap) => {
  const { sessionId } = snap.data();
   if (sessionId) {
     // We have a session, let's redirect to Checkout
     // Init Stripe
     const stripe = await getStripe();
     stripe.redirectToCheckout({ sessionId });
   }
});

How can I get the newly created document's ID list as // TODO above?

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

0

The addDoc function returns a Promise<DocumentReference> to the newly added document. So you cna get the ID with:

const docRef = await addDoc(collectionRef, payload);

console.log(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