首页 > 解决方案 > Firebase 未使用 .doc 上传到 Firestore

问题描述

我正在尝试使用firebase和stripe根据此处找到的源代码在我的网站上设置付款:https ://github.com/firebase/functions-samples/tree/master/stripe 我已经尝试重置我的规则firestore 和实时数据库,但我一直卡在 .snapshot 上。我查看了调试器,currentUser.uid 不为空,但它没有在数据库中创建新条目。对此的任何帮助将不胜感激!第一次使用firebase所以我有点迷茫。目前,我的规则文件与 github repo 中链接的文件相同。

firebase.auth().onAuthStateChanged((firebaseUser) => {
  if (firebaseUser) {
    currentUser = firebaseUser;
    firebase
      .firestore()
      .collection('stripe_customers')
      .doc(currentUser.uid)
      .onSnapshot((snapshot) => {
        if (snapshot.data()) {
          customerData = snapshot.data();
          startDataListeners();
          document.getElementById('loader').style.display = 'none';
          document.getElementById('content').style.display = 'block';
        } else {
          console.warn(
            `No Stripe customer found in Firestore for user: ${currentUser.uid}`
          );
        }
      });
  } else {
    document.getElementById('content').style.display = 'none';
    firebaseUI.start('#firebaseui-auth-container', firebaseUiConfig);
  }
});

标签: javascripthtmldatabasefirebasegoogle-cloud-firestore

解决方案


您显示的代码中绝对没有任何内容甚至尝试创建文档 - .doc() 只是创建对现有文档的引用。

.onSnapshot 为现有文档的首次读取和后续更改建立侦听器;我不会花时间在 GitHub 上搜索“startDataListeners()”(即使我实际上知道具体的存储库)

如果您希望此代码片段创建一个文档,我建议您花更多时间了解此代码到底在做什么。我强烈怀疑您使用的代码示例期望用户文档已经存在(即您的应用程序创建了它们),并且只是试图将您现有的用户连接到 Stripe 事务


推荐阅读