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?
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.