上传后 Firebase 删除文档。json
Firebase delete doc upon uploading .json
在 Firebase 中,我想在比较上传文件中的数据时删除集合中的文档。请问函数怎么写?
示例:文件“mail_addresses_06/14/21.json”已上传到 Storage/Import。数据包含:
{
"EmployeesOut" : [
"john.example@mail.com",
"liz.example@mail.com",
"eve.example@mail.com"
]
}
从 Firestore 数据库中的集合“UserWhiteList”中删除与这些邮件地址同名的文档
非常感谢,JR
以下 Cloud Function 代码应该可以解决问题:
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();
});
查看相应的文档项:
在 Firebase 中,我想在比较上传文件中的数据时删除集合中的文档。请问函数怎么写?
示例:文件“mail_addresses_06/14/21.json”已上传到 Storage/Import。数据包含:
{
"EmployeesOut" : [
"john.example@mail.com",
"liz.example@mail.com",
"eve.example@mail.com"
]
}
从 Firestore 数据库中的集合“UserWhiteList”中删除与这些邮件地址同名的文档
非常感谢,JR
以下 Cloud Function 代码应该可以解决问题:
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();
});
查看相应的文档项: