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

166
Visualizações
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 Respostas
Responde à pergunta

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 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