首页 > 解决方案 > 根据规则删除firestore文档

问题描述

我创建了一个应用程序,人们可以在其中上传一些自己的图像。

现在,为了处理人们可以上传不当图片的情况,我创建了一个报告系统。我所做的基本上是每次有人报告图像时,报告人的 ID 都会firestore像这样添加到数组中:

db
    .collection( "Reports" )
    .document( ImageID )
    .update( "Reports", FieldValue.arrayUnion( UID ) );

现在,我想设置一个规则,例如,如果数组的大小为 5(5 个不同的人报告图像),它将自动从云中删除该图像。

有什么办法可以代替每次读取数组并检查 ist 大小吗?

谢谢

标签: javafirebasegoogle-cloud-firestore

解决方案


Reports您可以在您的集合上创建触发器。

export const updateReportTrigger = functions.firestore
  .document('Reports/{ImageID}')
  .onUpdate(onUpdate)

async function onUpdate({ before, after }, context) {
  const newData = after.data()
  if (newData && newData.Reports && newData.Reports.length > 5) {
    // put your delete logic here
    // you can access the document id through context.params.ImageID
  }
}

推荐阅读