【发布时间】:2021-06-24 11:56:56
【问题描述】:
我有一个谷歌云功能,目前适用于 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 在实时数据库上的批量写入等价物是多路径更新,您将路径和值传递给写入
update()函数。如果您将null指定为路径的值,则该路径将被删除。转换整个函数有点超出 Stack Overflow 的范围,但这应该是一个很好的起点。
标签: javascript firebase firebase-realtime-database google-cloud-firestore google-cloud-functions