【问题标题】:Delete document in Firestore with Cloud Firestore update trigger使用 Cloud Firestore 更新触发器删除 Firestore 中的文档
【发布时间】:2020-09-17 12:34:01
【问题描述】:

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

我的主要目标是删除一个文档“party” 当派对上没有人时。但是,我不知道如何从 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()。抱歉,如果我遗漏了什么。
  • 天哪,我确信我已经尝试过了。又试了一次,它奏效了。请把它写成答案,我会接受的。

标签: node.js google-cloud-firestore google-cloud-functions


【解决方案1】:

您是否尝试过change.after.ref,这是文档参考?然后你可以这样做change.after.ref.delete()

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2018-09-21
    • 2021-03-16
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2018-03-27
    相关资源
    最近更新 更多