首页 > 解决方案 > Swift Firebase 函数 Javascript

问题描述

我正在寻求帮助以了解如何将数据添加到 firebase 函数。

exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => {
const customer = await stripe.customers.create({ email: user.email });
const intent = await stripe.setupIntents.create({
customer: customer.id,
});
await admin.firestore().collection('stripe_customers').doc(user.uid).set({
customer_id: customer.id,
setup_secret: intent.client_secret,
});
return;
});

上述功能有效,但我可以为我的应用程序创建 3 种不同类型的配置文件,我不希望在每个用户登录时都调用它。

exports.createStripeCustomer = functions.https.onCall(async (data, context) => {
const full_name = data.full_name;
const email = data.email;
const customer = await stripe.customers.create({
  email: email,
  name: full_name,
  description: full_name
});
console.log('new customer created: ', customer.id)
await admin.firestore().collection('stripe_customers').doc(user.uid).set({
customer_id: customer.id,
setup_secret: intent.client_secret,
});
await admin.database().ref(`/user_profiles/${user.uid}/customer_id`).set(customer.id);
return {
  customer_id: customer.id
}
});

上面的代码适用于在条带中创建客户帐户,但不执行将数据保存到 Firestore 和实时数据库中。

func createCustomerId(email: String, completion: ((String?, Error?) -> Void)?){
                            let data: [String: Any] = [
                                "email": email,
                                "name": name
                            ]
                            print("calling firebase function to create stripe customer")
                            
                            Functions.functions().httpsCallable("createStripeCustomer")
                                .call(data) { result, error in

                                    if let error = error {
                                        completion!(nil, error)
                                    } else if let data = result?.data as? [String: Any],
                                        let customerId = data["customerId"] as? String {
                                        completion!(customerId, nil)
                                    }
                            }
                        }

上面的代码是我在客户登录期间调用我的 firebase 函数的方式。我做错了什么阻止数据保存到firebase?

标签: javascriptswiftfirebasegoogle-cloud-functions

解决方案


推荐阅读