【发布时间】:2020-12-19 23:28:50
【问题描述】:
我已在 Firebase 中安装了“使用 Stripe 运行订阅付款”扩展程序。
当我尝试为用户订阅计划时,我收到一条错误消息,提示“snap.data 不是函数”。我使用以下代码:
db
.collection('customers')
.doc(cred.user.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
}).then(() => {
// Wait for the CheckoutSession to get attached by the extension
db
.collection('customers')
.doc(cred.user.uid)
.collection('checkout_sessions').onSnapshot((snap) => {
console.log(snap)
const { sessionId } = snap.data();
if ({sessionId}) {
console.log(sessionId)
// We have a session, let's redirect to Checkout
// Init Stripe
const stripe = Stripe('pk_test_ZEgiqIsOgob2wWIceTh0kCV4001CPznHi4');
stripe.redirectToCheckout({ sessionId });
};
});
});
这是 Firebase 团队提供的代码示例的 .then 变体:
const docRef = await db
.collection('customers')
.doc(currentUser.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
});
// Wait for the CheckoutSession to get attached by the extension
docRef.onSnapshot((snap) => {
const { sessionId } = snap.data();
if (sessionId) {
// We have a session, let's redirect to Checkout
// Init Stripe
const stripe = Stripe('pk_test_1234');
stripe.redirectToCheckout({ sessionId });
}
});
我的 await 函数出错,提示“未捕获的 SyntaxError:await 仅在异步函数中有效”。所以我将代码重构为 .then 变体。
当我从 button.addEventListener("click) 运行此代码时,我收到一条错误消息,提示 snap.data 不是函数。
我检查了 snap,它已被填充
客户在 Firestore 和 Stripe 仪表板中创建,客户会话被创建。
这里出了什么问题? Stackoveflow (Run Subscription Payments with Stripe Firebase Extension Webhook Not Firing) 上有一个类似的问题,但没有答案,所以我再试一次。
【问题讨论】:
标签: javascript firebase google-cloud-firestore stripe-payments