简体   繁体   English

使用 Cloud Functions 从 Firestore 中删除文档

[英]Deleting document from Firestore with Cloud Functions

I am trying to make this Cloud Function to work.我正在尝试使此 Cloud Function 工作。 I would like to delete a document from Firestore that has as name the previous date of function execution.我想从 Firestore 中删除一个名为 function 执行日期的文档。 This function is supposed to be executed every night at 2am, but for some reason it is not working.这个 function 应该在每晚凌晨 2 点执行,但由于某种原因它不起作用。 Here is my code:这是我的代码:

exports.deleteYesterday = functions.pubsub
    .schedule("0 02 * * *")
    .onRun(async (context) => {
        var yesterday = new Date();
        var dd = String(yesterday.getDate() - 1).padStart(2, "0");
        var mm = String(yesterday.getMonth() + 1).padStart(2, "0"); 
        var yyyy = yesterday.getFullYear();

        const dateString = (yyyy + "-" + mm + "-" + dd).toString();

        let ref = admin.firestore().collection("citas").doc(dateString);
        return ref.delete();
    });

When logging dateString value I get the correct date format I need: dateString记录 dateString 值时,我得到了我需要的正确日期格式: dateString

This is how the database looks like: documents这是数据库的样子:文档

Thanks!!谢谢!!

Your database screenshot shows that there is no document called "citas/2020-12-02".您的数据库屏幕截图显示没有名为“citas/2020-12-02”的文档。 It was likely already deleted.它可能已经被删除了。 You can tell because the name of the document is in italics.你可以知道,因为文件的名称是斜体的。 That italics means that there is no document, but there are nested subcollections organized under it.该斜体表示没有文档,但在其下组织了嵌套的子集合。 Those subcollections will not be deleted when you delete the parent document.当您删除父文档时,这些子集合不会被删除。 You will have to write some code to delete all of the documents in the subcollections if you want it to disappear.如果您希望它消失,您将不得不编写一些代码来删除子集合中的所有文档。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM