简体   繁体   English

将 StripeID 保存到 Firestore 集合文档

[英]Saving StripeID to Firestore Collection Document

I'm switching data from Firebase Realtime Database to Firestore because I need more querying capabilities.我正在将数据从 Firebase 实时数据库切换到 Firestore,因为我需要更多查询功能。 However, I'm having trouble with saving my customer's stripeID to their collection document.但是,我在将客户的stripeID到他们的收集文档时遇到了问题。 My Cloud Function is hitting perfectly because Stripe is creating the customer correctly, but it's not assigning to the collection reference.我的云 Function 完美命中,因为 Stripe 正确创建了客户,但没有分配给集合引用。 What do I need to fix so the collection reference could recognize the stripeID as well?我需要修复什么以便集合引用也可以识别stripeID Thank you!谢谢!

What I'm Seeing我看到了什么

在此处输入图像描述

在此处输入图像描述

Customer Model客户 Model

struct Customer {
    
    let uid: String
    let stripeId: String
    let fullname: String
    let email: String
    let username: String
    let profileImageUrl: String
    
    init(dictionary: [String : Any]) {
        self.uid = dictionary["uid"] as? String ?? ""
        self.stripeId = dictionary["stripeId"] as? String ?? ""
        self.fullname = dictionary["fullname"] as? String ?? ""
        self.email = dictionary["email"] as? String ?? ""
        self.username = dictionary["username"] as? String ?? ""
        self.profileImageUrl = dictionary["profileImageUrl"] as? String ?? ""
    }
    
}

AuthServices授权服务

struct CustomerCredentials {
    let email: String
    let password: String
    let fullname: String
    let username: String
    let profileImage: UIImage
}

static func createCustomer(credentials: CustomerCredentials, completion: CollectionCompletion) {

    Auth.auth().createUser(withEmail: credentials.email, password: credentials.password) { result, error in
                if let error = error {
                    debugPrint(error.localizedDescription)
                    return
                }
                    
                guard let uid = result?.user.uid else { return }
                    
                let values = ["uid" : uid,
                              "email" : credentials.email,
                              "fullname" : credentials.fullname,
                              "username" : credentials.username,
                              "profileImageUrl" : profileImageUrl]
                    
                REF_CUSTOMERS.document(uid).setData(values, completion: completion)
            }
    }

}

RegistrationController注册控制器

@objc func handleCreateAccount() {

    let credentials = CustomerCredentials(email: email, password: password, fullname: fullname,
                                          username: username, profileImage: profileImage)
        
    AuthService.createCustomer(credentials: credentials) { error in
        if let error = error {
            Auth.auth().handleFireAuthError(error: error, vc: self)
            self.showLoader(false)
            return
        }
            
        Functions.functions().httpsCallable("createStripeCustomer").call(["email" : email]) { result, error in
            if let error = error {
                debugPrint(error.localizedDescription)
                return
            }
        }

    }

}

Assuming that REF_CUSTOMERS points to the "customers" collection, then when you're using the following line of code:假设REF_CUSTOMERS指向“客户”集合,那么当您使用以下代码行时:

REF_CUSTOMERS.document(uid).setData(values, completion: completion)

For writing the following values:用于写入以下值:

let values = ["uid" : uid,
              "email" : credentials.email,
              "fullname" : credentials.fullname,
              "username" : credentials.username,
              "profileImageUrl" : profileImageUrl]

The document that will be added will always contain only the data that exists in the values object. Since there is no stripeId present, the document that will be written will not contain the stripeId .将添加的文档将始终包含values object 中存在的数据。由于不存在stripeId ,因此将写入的文档将不包含stripeId If you need to have the stripeId as a field in the user document, then you have to add it like this:如果您需要将stripeId作为用户文档中的一个字段,那么您必须像这样添加它:

let values = ["uid" : uid,
              "email" : credentials.email,
              "fullname" : credentials.fullname,
              "username" : credentials.username,
              "profileImageUrl" : profileImageUrl
              "stripeId" : stripeId] //👈

In my RegistrationController, I had to add an updateData value inside my Cloud Function code snippet in order to add it to my customer reference.在我的 RegistrationController 中,我必须在 Cloud Function 代码片段中添加一个updateData值,以便将其添加到我的客户参考中。

RegistrationController注册控制器

Functions.functions().httpsCallable("createStripeCustomer").call(["email" : email]) { result, error in
      if let error = error {
          debugPrint(error.localizedDescription)
          return
      }
                
      let value = ["stripeId" : result?.data]
                
      guard let uid = Auth.auth().currentUser?.uid else { return }
                
      REF_CUSTOMERS.document(uid).updateData(value as [AnyHashable : Any])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM