简体   繁体   English

使用条带结帐时出现未定义的错误和 url

[英]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.

相关问题 Apple 通过 Stripe Checkout 支付 - Apple pay with Stripe Checkout Firebase 部署错误:将单独的项目与 Stripe“使用 Stripe 运行付款”扩展一起使用时,“不支持更改机密” - Firebase Deploy Error: 'Changing secrets is not supported' when using separate projects with Stripe 'Run Payments with Stripe' extension 使用 FieldValue 或 FirestoreAdminClient 时未定义 - Getting undefined when using FieldValue or FirestoreAdminClient 使用 Apple ID 登录 Firebase 时出现错误“invalid_request: Invalid web redirect url” - Getting error "invalid_request: Invalid web redirect url" when using Apple ID sign-in with Firebase Stripe Checkout:出了点问题 - Stripe Checkout: Something went wrong 在 nodejs 后端使用 msal 时出错 - Getting an error when using msal in a nodejs backend Firebase Stripe Extension:结帐按钮未重定向到 stripe live 模式 - Firebase Stripe Extension: checkout button not redirecting to stripe live mode Firebase 用户的条带扩展结帐链接 - Firebase Stripe extension checkout link for user Firebase 调用 Stripe 时函数出错 - Firebase Functions error when calling Stripe Dropbox url 复制到 azure blob 存储时出错 - Getting error when Dropbox url copy to azure blob storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM