繁体   English   中英

使用 Cloud Firestore 更新触发器删除 Firestore 中的文档

[英]Delete document in Firestore with Cloud Firestore update trigger

我正在使用 Cloud Firestore 触发器。
https://firebase.google.com/docs/functions/firestore-events

我的主要目标是在聚会上没有人时删除一个文件,即“聚会”。 但是,我不知道如何从 Cloud Firestore 触发器执行此操作。 这是我的代码:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

exports.deleteOnCheckout = functions.firestore.document('/parties/{documentId}')
    .onUpdate((change, context) => {
      // Get an object representing the document
      const newValue = change.after.data();
      // ...or the previous value before this update
      const previousValue = change.before.data();

      // access a particular field as you would any JS property
      const checkedIn = newValue.checkedIn;
      console.log("Array size: " + checkedIn.length);
      //If no checked in, delete document
      if(checkedIn.length == 0){
            return //Here I want to delete the document
      }else{
          //Do nothing
          return null;
      }
    });

您是否尝试过文档引用的change.after.ref 然后你可以像这样change.after.ref.delete()那样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM