[英]Getting undefined for error and url when using stripe checkout
I have the a createCheckoutSession.ts
:我有一个
createCheckoutSession.ts
:
import { db } from '../firebase/firebaseInit';
import { collection, addDoc, onSnapshot } from 'firebase/firestore';
import { User } from 'firebase/auth';
export async function createCheckoutSession(user: User) {
console.log('clicked')
const docRef = await addDoc(collection(db, `users/${user?.uid}/checkout_sessions`), {
price: 'price_1Lb3hiKi0c1bV0RosKIhQ699',
success_url: `http://localhost:3000/success`,
cancel_url: `http://localhost:3000/cancel`,
});
console.log(docRef)
onSnapshot(docRef, (snap) => {
console.log('dsfdsfdf')
const { error, url } = snap.data();
console.log(error, 'snap')
if (error) {
// Show an error to your customer and
// inspect your Cloud Function logs in the Firebase console.
alert(`An error occured: ${error.message}`);
}
if (url) {
// We have a Stripe Checkout URL, let's redirect.
window.location.assign(url);
}
});
}
In vscode I get an error for the line const { error, url } = snap.data();
在 vscode 中,我收到一条错误
const { error, url } = snap.data();
: :
Property 'error' does not exist on type 'DocumentData | undefined'.ts(2339)
Error from console:来自控制台的错误:
Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
Security rules:安全规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth.uid == uid;
match /checkout_sessions/{id} {
allow read, write: if request.auth.uid == uid;
}
match /subscriptions/{id} {
allow read: if request.auth.uid == uid;
}
}
match /products/{id} {
allow read: if true;
match /prices/{id} {
allow read: if true;
}
match /tax_rates/{id} {
allow read: if true;
}
}
}
}
So I'm getting undefined for the error and url... any ideas what I'm doing wrong?所以我对错误和 url 没有定义......任何想法我做错了什么?
The snap.data()
would be undefined
in case the document does not exist and you cannot read properties of undefined .如果文档不存在并且您无法读取 undefined 的属性,则
snap.data()
将是undefined
的。 You should check if the document exists as shown below:您应该检查该文件是否存在,如下所示:
onSnapshot(docRef, snap => {
console.log("In onSnapshot");
// check if the document exists
if (snap.exists()) {
const { name, error } = snap.data(); // This will never be undefined
} else {
console.log("Document does not exist");
}
});
The security rules should allows users to read/write their own data so you can add the document.安全规则应允许用户读取/写入他们自己的数据,以便您可以添加文档。 Try:
尝试:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId}/{data=**} {
allow read, write: if request.auth.uid == userId;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.