Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!