繁体   English   中英

将 StripeID 保存到 Firestore 集合文档

[英]Saving StripeID to Firestore Collection Document

我正在将数据从 Firebase 实时数据库切换到 Firestore,因为我需要更多查询功能。 但是,我在将客户的stripeID到他们的收集文档时遇到了问题。 我的云 Function 完美命中,因为 Stripe 正确创建了客户,但没有分配给集合引用。 我需要修复什么以便集合引用也可以识别stripeID 谢谢!

我看到了什么

在此处输入图像描述

在此处输入图像描述

客户 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 ?? ""
    }
    
}

授权服务

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)
            }
    }

}

注册控制器

@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
            }
        }

    }

}

假设REF_CUSTOMERS指向“客户”集合,那么当您使用以下代码行时:

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

用于写入以下值:

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

将添加的文档将始终包含values object 中存在的数据。由于不存在stripeId ,因此将写入的文档将不包含stripeId 如果您需要将stripeId作为用户文档中的一个字段,那么您必须像这样添加它:

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

在我的 RegistrationController 中,我必须在 Cloud Function 代码片段中添加一个updateData值,以便将其添加到我的客户参考中。

注册控制器

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