【发布时间】: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