2 回答

TA貢獻1876條經驗 獲得超5個贊
如文檔中所述:
對于 和 事件,參數具有前后字段。這些都是 .
onWrite
onUpdate
change
DataSnapshot
因此,您可以執行以下操作:
exports.checkPrivate = functions.firestore
.document('public_posts/{postid}').onUpdate((change, context)=>{
const data=change.after.data();
if (!data.public) { //Note the additional change here
const docRef = change.after.ref;
return docRef.delete();
}
else {
return null;
}
});
在下面更新以下Karolina Hageg?rd評論:如果要獲取通配符的值,則需要使用對象,如:.postidcontextcontext.params.postid
嚴格來說,您獲得的是文檔 ID,而不是其 .當然,基于此值,您可以重新構建 with 將給出與 相同的對象。DocumentReferenceDocumentReferenceadmin.firestore().doc(`public_posts/${postid}`);change.after.ref

TA貢獻1890條經驗 獲得超9個贊
onUpdate 偵聽器返回一個對象 (https://firebase.google.com/docs/reference/functions/cloud_functions_.changeChange
)
要獲取更新的文檔,您需要執行以下操作:
change.after.val()
要刪除文檔,您需要執行以下操作:
change.after.ref.remove()
添加回答
舉報