【问题标题】:Swift Firestore calling data - Values do not exist after creating accountSwift Firestore 调用数据 - 创建帐户后值不存在
【发布时间】:2021-06-21 03:43:59
【问题描述】:

使用 Firebase 创建帐户后,我将值保存到 Firestore。我想在用户创建帐户后获取这些数据并将其保存到实时数据库中。经过测试,我注意到创建帐户后,即使将值保存到 Firestore,这些值也不存在。在我注销帐户并重新登录后,现在可以访问这些值。可能是什么问题?

Swift Firebase Firestore 调用数据

func callCustomerStripeInfo() {
    Firestore.firestore().collection("stripe_customers").document(customerUid!).getDocument { (snapshot, err) in
           if let err = err {
               print("Error getting documents: \(err)")
           } else {
            let stripeInfo = snapshot?.data()
            let stripe_customer_id = stripeInfo!["customer_id"] as! String
            let stripe_setup_secret = stripeInfo!["setup_secret"] as! String
            print(stripe_customer_id)
            print(stripe_setup_secret)
            Database.database().reference().child("user_profiles").child(self.customerUid!).updateChildValues(["customer_id":stripe_customer_id, "setup_secret":stripe_setup_secret])
           }
     }
}

Firestore 规则

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
  allow read;
  allow write: if false;
}
}
}

【问题讨论】:

  • 如果您尝试使用硬编码值写入实时数据库,而不是先从 Firestore 读取它们,您是否也有同样的问题?
  • 这个问题有点含糊——即使保存了这些值,这些值也不存在是什么意思?具体来说,什么不存在,你怎么知道它们不存在?另外,这不是主题,但是为什么要使用两个不同的数据库来存储相同的数据?为什么不坚持使用 Firestore(或 RTDB 来简化事情?
  • 我已将 Firestore 调用移至比您在注册后在该应用程序中显示的第一个视图更合适的视图,并且正确调用了值。我正在使用 RTDB 和 Firestore,因为 RTDB 拥有所有数据,而我将 Firestore 用于条带化。
  • 这是否意味着您已经解决了问题?如果不是,你能澄清一下这个问题吗?

标签: swift firebase firebase-realtime-database google-cloud-firestore


【解决方案1】:

为了使用 Cloud Firestore 获得实时更新,您必须使用 addSnapshotListener() 方法。更多信息请查看this documentation。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2020-01-05
    • 2018-04-27
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多