gpt4 book ai didi

javascript - Firebase Firestore - 在 Stripe.CreateCustomer 之后通过 Cloud Function 更新 Firestore 数据库

转载 作者:行者123 更新时间:2023-12-02 21:23:37 26 4
gpt4 key购买 nike

在进行 OnCreate 调用以创建 strip 用户后,我尝试更新我的数据库,但我遇到了问题。这应该是一项简单的任务,但我似乎错过了一些东西。

在我的 SignUpActivity.java 文件中,我将字段 userStripeCusIduserStripeAcctId 创建为 null,然后尝试稍后使用以下命令更新字段值我的云函数 https 调用。

我知道它会触发,因为它在 Stripe 数据库中创建了一个新用户,并且我让它记录了新创建的 Stripe 客户 ID,这就是我试图保存在 Firestore 数据库中的内容。

有人知道如何解决这个问题吗?

我一直在看这个 YouTube 视频作为引用,但没有任何运气。 YouTube 视频使用实时数据库,而不是我正在使用的 Cloud Firestore。

SignUpActivity.java

private void registerUser() {

userDocRef.set(userInfo).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {

//time stamp block
Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm a", Locale.US);
String userCreatedTimestamp = formatter.format(date);
System.out.println("Today : " + userCreatedTimestamp);

userDocRef.update(
"firstName", firstName,
"lastName", lastName,
"userEmail", userEmail,
"userName", userName,
"userPhoneNumber", completePhoneNumber,
"userStripeCusId", null, // <--FIELD CREATED HERE!
"userStripeAcctId", null, // <--FIELD CREATED HERE!
"userCreatedTimestamp", userCreatedTimestamp).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: userinfo was created");
}
});
Log.d(TAG, "onSuccess: user was created");
startActivity(new Intent(SignUpActivity.this, DashboardActivity.class));
finish();

}
})

createStripeCustomer();

private void createStripeCustomer() {

mFunctions.getHttpsCallable("createStripeCustomerHTTPSCall").call(data);

}


index.js

exports.createStripeCustomerHTTPSCall = functions.firestore.document('Users/{userName}').onCreate((snapshot, context) =>{

console.log("Stripe Customer profile for " + context.params.userName + " created.");
// ^ returns username correctly

return stripeToken.customers.create(
{
description: 'My First Test Customer (created for API docs)',
email: context.params.userName,
},
(err, StripeCustomer) => {

console.log("customer id: " + StripeCustomer.id + " customer email: " + StripeCustomer.email);
//^ returns correct stripe customer ID and associated email it was created with.

console.log("Creating Stripe customer failed because: " + err.toString())
admin.database().ref('Users/{userName}').update({ "userStripeCusId": StripeCustomer.id})
// no action is triggered. Stripe customerID is not saved in my firebase DB

// ^ @DougStevenson pointed out that using admin.database() is meant for a REALTIME DATABASE which is not my case.

}
).catch(error => {
console.log(error)
})
});

--------------------------更新-------------------- ---

index.js 已更新为此。

exports.createStripeCustomerHTTPSCall = functions.firestore.document('Users/{userName}').onCreate((snapshot, context) =>{

console.log("Stripe Customer profile for " + context.params.userName + " created.");
console.log("Stripe Customer Email for " + context.params.userEmail + " created.");

return stripeToken.customers.create(
{
description: 'My First Test Customer (created for API docs)',
email: context.params.userName,
},
(err, StripeCustomer) => {
// asynchronously called
console.log("customer id: " + StripeCustomer.id + " customer email: " + StripeCustomer.email);
// admin.database().ref('Users/{userName}').update({ "userStripeCusId": StripeCustomer.id}) // <-- admin.database() is for REALTIME DATABASE

console.log("snapshot.get-userStripeCusId-: " + snapshot.get("userStripeCusId"));

// console.log("snapshot.get(Users/{userName}/userStripeCusId): " + snapshot.get("Users/{userName}/userStripeCusId")); <-- doesnt work
console.log("snapshot.data(): " + snapshot.data().toString()); // <-- returns object
console.log("snapshot.data().userName: " + snapshot.data().userName); // <-- returns correct userName
console.log("snapshot.data().userEmail: " + snapshot.data().userEmail); // <-- returns correct userEmail

我已经尝试过以下方法,但没有成功。

1) return admin.firestore.ref.update({"userStripeCusId": StripeCustomer.id})

尽管此 Firebase video 中建议这样做,但这不起作用8:07

2) return admin.firestore().collection('Users').doc(snapshot.data().userEmail).update({"userStripeCusId": StripeCustomer.id});
也尝试过此操作,如StackOverflow Post所示

我也尝试过关注 this Firestore / Stripe source但运气不好

最佳答案

您正在写入实时数据库而不是 Firestore:

admin.database().ref('Users/{userName}').update({ "userStripeCusId": StripeCustomer.id})

admin.database() 不允许您访问 Firestore。请改用 admin.firestore() (请注意,它具有完全不同的 API)。

此外,您没有处理从该(错误)更新返回的 promise ,因此 Cloud Functions 不知道要等待它完成。

关于javascript - Firebase Firestore - 在 Stripe.CreateCustomer 之后通过 Cloud Function 更新 Firestore 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60796169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com