【问题标题】:Data model for Stripe payments in FirestoreFirestore 中 Stripe 付款的数据模型
【发布时间】:2018-06-17 21:22:35
【问题描述】:

我正在关注 Google 提供的将 Stipe 与 RTDB 结合使用的教程 (https://angularfirebase.com/lessons/collect-payments-with-angular-stripe-checkout-and-firebase/),并尝试调整该示例以与 Firestore 一起使用。

本教程进行以下调用以将数据添加到 RTDB:

const payment = { token, amount }
return this.db.list(`/payments/${this.userId}`).push(payment)

我对如何在 Firestore 中执行此操作的最佳猜测是:

const payment = {token, 'price':this.props.price}
var db = firebase.firestore()
db.collection('payments').doc(this.props.uid).collection('test').add(payment)

请注意,我有“测试”集合,因为 Firestore 要求您在文档和集合之间交替。这是在 Firestore 中构建数据的正确方法吗?

【问题讨论】:

    标签: firebase stripe-payments google-cloud-functions google-cloud-firestore


    【解决方案1】:

    我认为您可以使用示例中显示的相同结构:

    payments<collection>
        userId<doc>
            paymentId<field> : [
                                amount: number
                                token: object
                                charge: object 
                               ]
    

    请注意,这里的令牌被视为地图。此解决方案是否符合您的需求?

    【讨论】:

    • 我尝试实现这个想法,但遇到了问题。这是我尝试运行的内容:db.collection('payments').doc(this.props.uid).update({ paymentId: payment })
    • 有什么问题?是设计问题(您想要实现其他目标)还是出现错误?
    • 我能够看到数据,但现在我遇到了一个新问题:当我使用新字段更新文档时,我不清楚如何使 paymentId 动态化。目前我的firestore数据库有字段'{paymentId}'const payment = {token, 'price':this.props.price} const paymentId = token.id var db = firebase.firestore() db.collection('payments').doc(this.props.uid).set({ '{paymentId}': payment }, { merge: true })
    【解决方案2】:

    当我将 Stripe 支付数据从 Realtime 移动到 Firestore 时,我遇到了类似的情况。

    我解决它的方法是让我的顶级集合是 users,每个文档都是一个 user uid,每个用户文档都有一个付款集合我将新的付款添加到此集合中。

    我更喜欢这种方法而不是将字段作为数组,因为它更容易查询。

    例子:

    db.collection('users').doc(this.props.uid).collection('payments').add(payment);
    

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 2018-09-12
      • 2017-04-16
      • 2023-04-11
      • 2020-01-10
      • 2015-03-01
      • 2018-10-09
      • 2021-10-13
      • 2017-06-03
      相关资源
      最近更新 更多