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



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

我的主要目标是删除一个文件;聚会;当聚会上没有人的时候。然而,我不知道如何从云火库触发器。这是我的代码:

// 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()那样做。

相关内容

  • 没有找到相关文章

最新更新