在一定时间后更新 Firebase Firestore 数据

Update Firebase Firestore Data After A Certain Amount of Time

我想在创建特定时间后更新我的 firestore 插入的字段。我搜索了几种可能性并提出了云功能。不幸的是,为了编写该函数,java 脚本对我来说并不那么容易理解。另外我红了,反正过了一段时间也不可能执行代码了。

那么如何在不编写大量js代码并为使用该功能付费的情况下处理该问题。也许第二个应用程序只是一个周期性计时器并一次又一次地检查 DateTime 字段。 (我可以 运行 该应用程序 24/7)

谢谢大家

您可以创建一个在创建文档时触发的云函数。您可以使用像 setTimeout() 这样的计时器来执行更改文档的功能。

看下面的示例代码:

function updateDoc(id) {
    admin.firestore()
    .collection('Orders')
    .doc(id)
    .update({status: 2})
    .then(() => {
        console.log("Document ID: "+id+" successfully updated!");
    })
    .catch((error) => {
        console.error("Error updating document: ", error);
    });
}

exports.updateField = functions.firestore
    .document('/Orders/{id}')
    .onCreate((snapshot, context) => {
        console.log(context.params.id);
        const id = context.params.id;
        setTimeout(() => updateDoc(id), 600000);
    });

这是我执行上述功能时的日志。

您可能需要检查 Cloud Firestore triggers for other triggers and setTimeout() 以了解可以在 use-case 中使用的其他超时选项。