Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何重新编写batch.delete函数以用于实时数据库?_Javascript_Firebase_Firebase Realtime Database_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 如何重新编写batch.delete函数以用于实时数据库?

Javascript 如何重新编写batch.delete函数以用于实时数据库?,javascript,firebase,firebase-realtime-database,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Firestore,Google Cloud Functions,我有一个谷歌云功能,目前为firestore工作。我能够将第一个变量转换为实时数据库,但我不太了解()如何将其转换为其他变量 如何在实时数据库中执行此操作? exports.cleanupUser = functions.auth.user().onDelete(async (user) => { const dbRef = admin.database().ref('stripe_customers');//admin.firestore().collection('strip

我有一个谷歌云功能,目前为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需要删除要删除的文档的每个子集合中的每个文档

简而言之,这个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();
  });

Firestore对实时数据库的批写操作的等效功能是多路径更新,您可以将要写入的路径和值传递给
update()
函数。如果指定
null
作为路径的值,则该路径将被删除。转换整个函数超出了堆栈溢出的范围,但这应该是一个很好的起点。