[英]Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js
I want to retrieve the field value from a document snapshot which is the URL of a file in firebase storage and delete the file from firebase storage also the firestore document if the time of creation of the doc is before 24 hrs.我想从文档快照中检索字段值,即 firebase 存储中文件的 URL 并从 firebase 存储中删除文件
I am able to delete expired firestore documents successfully with the code below:我可以使用以下代码成功删除过期的 Firestore 文档:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { firestore } = require("firebase-admin");
admin.initializeApp();
exports.removeExpiredDocuments = functions.pubsub.schedule("every 1 hours").onRun(async (context) => {
const db = admin.firestore();
const now = firestore.Timestamp.now();
const ts = firestore.Timestamp.fromMillis(now.toMillis() - 86400000); // 24 hours in milliseconds = 86400000
const snap = await db.collection("photos").where("timestamp", "<", ts).get();
let promises = [];
snap.forEach((snap) => {
promises.push(snap.ref.delete());
});
return Promise.all(promises);
});
but I don't know how to retrieve the field value(URL of file) from the document snapshot within the forEach block and delete the file from firebase storage.但我不知道如何从 forEach 块中的文档快照中检索字段值(文件的 URL)并从 firebase 存储中删除文件。
Here's the firestore database:这是firestore数据库:
The field value of photourl is to be retreived. photourl 的字段值将被检索。
I think code look like:我认为代码看起来像:
//some code ....
snap.docs.map((doc) => {
if (doc.exist) {
var url = doc.data().photourl;
//do something logic call to firestorage and deleted data base on url get
//write logic deleted url firebase after deleted success firestorage
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.