在Firebase中,我想在比较上传文件中的数据后删除集合中的文档。请问如何编写函数?
示例:文件";mail_addresses_06/14/21.json";已上载到存储/导入。数据包含:
{
"EmployeesOut" : [
"john.example@mail.com",
"liz.example@mail.com",
"eve.example@mail.com"
]
}
从集合中删除名称与那些邮件地址相同的文件";用户白名单";在Firestore数据库中
非常感谢,JR
下面的云函数代码应该可以做到这一点:
exports.deleteUsers = functions.storage.object().onFinalize(async (object) => {
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const file = admin
.storage()
.bucket(fileBucket)
.file(filePath);
const fileData = await file.download();
const contents = JSON.parse(fileData[0].toString());
const employeesOut = contents.EmployeesOut;
const db = admin.firestore();
const batch = db.batch();
for (const property in employeesOut) {
const docRef = db.collection("UserWhiteList").doc(employeesOut[property]);
batch.delete(docRef);
}
return batch.commit();
});
参见相应的文档项目:
- 在云存储更改时触发云功能
- 如何使用Google Cloud Storage Node.js客户端在内存中下载文件
- 删除的批处理写入(注意限制为500次操作(