如何重写这个batch.delete函数来为实时数据库工作



我有一个谷歌云功能,目前适用于firestore。我能够将第一个变量转换为实时数据库,但不太了解((对其余部分做同样的操作

如何在实时数据库中执行此操作

exports.cleanupUser = functions.auth.user().onDelete(async (user) => {
const dbRef = admin.database().ref('stripe_customers');//admin.firestore().collection('stripe_customers');
const customer = (await dbRef.doc(user.uid).get()).data();
await stripe.customers.del(customer.customer_id);
// Delete the customers payments & payment methods in firestore.
const batch = admin.firestore().batch();
const paymetsMethodsSnapshot = await dbRef
.doc(user.uid)
.collection('payment_methods')
.get();
paymetsMethodsSnapshot.forEach((snap) => batch.delete(snap.ref));
const paymentsSnapshot = await dbRef
.doc(user.uid)
.collection('payments')
.get();
paymentsSnapshot.forEach((snap) => batch.delete(snap.ref));

await batch.commit();

await dbRef.doc(user.uid).delete();
return;
});

我尝试了以下操作,但似乎没有奏效:

exports.cleanupUser = functions.auth.user().onDelete(async (user) => {
const dbRef = admin.database().ref('stripe_customers');//admin.firestore().collection('stripe_customers');
const customer = (await dbRef.child(user.uid).get()).data();
await stripe.customers.del(customer.customer_id);
// Delete the customers payments & payment methods in firestore.
const batch = admin.database().batch();
const paymetsMethodsSnapshot = await dbRef
.child(user.uid).child('payment_methods').get();
paymetsMethodsSnapshot.forEach((snap) => batch.delete(snap.ref));
const paymentsSnapshot = await dbRef
.child(user.uid).child('payments').get();
paymentsSnapshot.forEach((snap) => batch.delete(snap.ref));

await batch.commit();

await dbRef.child(user.uid).delete();
return;
});

实时数据库的数据结构比Firestore简单得多,Firestore有优点也有缺点。

对于实时数据库,如果删除某个位置的数据,则该位置下的所有数据都将被删除。这与Firestore不同,在Firestore中,您需要删除要删除的文档的每个子集合中的每个文档。

简而言之,这个消防仓库代码

const dbRef = admin.firestore().collection('stripe_customers');
const batch = admin.firestore().batch();
const paymetsMethodsSnapshot = await dbRef
.doc(user.uid)
.collection('payment_methods')
.get();
paymetsMethodsSnapshot.forEach((snap) => batch.delete(snap.ref));
const paymentsSnapshot = await dbRef
.doc(user.uid)
.collection('payments')
.get();
paymentsSnapshot.forEach((snap) => batch.delete(snap.ref));
await batch.commit();
await dbRef.doc(user.uid).delete();

变平为仅

const dbRef = admin.database().ref('stripe_customers');
await dbRef.child(user.uid).remove();

你的功能是什么:

exports.cleanupUser = functions.auth.user()
.onDelete(async (user) => {
const dbRef = admin.database().ref('stripe_customers');
// Firestore's .get() is the same as RTDB's .once('value')
// Firestore's .data() is the same as RTDB's .val()
const customer = (await dbRef.child(user.uid).once('value')).val();
// user data already deleted? do nothing rather than throw errors.
if (customer === null) {
return;
}
await stripe.customers.del(customer.customer_id);
await dbRef.child(user.uid).remove();
});

相关内容

  • 没有找到相关文章

最新更新