简体   繁体   English

根据同一文档中的时间戳属性更新 Firestore 文档上的属性

[英]Update Property on Firestore Document Based on a timestamp property in same document

I have a Firestore Document which has these 2 properties:我有一个具有以下两个属性的 Firestore 文档:

isActive: true lastDateForSubmission: a timestamp. isActive: true lastDateForSubmission: 时间戳。

I am using the isActive property to enable or disable upload in my React Application.我正在使用 isActive 属性在我的 React 应用程序中启用或禁用上传。 Currently the lastDateForSubmission is just for display.目前 lastDateForSubmission 仅用于显示。

I dont have any backend servers.我没有任何后端服务器。 I am using plain React and Firebase.我正在使用普通的 React 和 Firebase。

I need to automatically set the isActive property to False after the time in the LastDateForSubmission property.我需要在 LastDateForSubmission 属性中的时间之后自动将 isActive 属性设置为 False。

I have been searching for a solution for a long time that doesnt involve back end servers and i haven't been able to find one.我一直在寻找不涉及后端服务器的解决方案,但我一直无法找到。

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

You can not do such things without a server.如果没有服务器,您将无法执行此类操作。 But fortunately you don't need to maintain a server Firebase has firebase cloud functions for that.但幸运的是,您不需要维护服务器Firebase具有 firebase 云功能。

You can schedule tasks and update your database.您可以安排任务并更新数据库。 Look at https://firebase.google.com/docs/functions/schedule-functions看看https://firebase.google.com/docs/functions/schedule-functions

Firestore is a database so it cannot execute code by itself. Firestore 是一个数据库,因此它不能自行执行代码。 You can however use firestore rules to determine whether writes are or aren't allowed, so whilst this would not change the database isActive value, it could still be used to prevent a write after the deadline as specified in your lastDateForSubmission variable.但是,您可以使用 firestore 规则来确定是否允许写入,因此虽然这不会更改数据库的isActive值,但它仍可用于防止在lastDateForSubmission变量中指定的截止日期之后进行写入。

https://firebase.google.com/docs/firestore/security/rules-conditions https://firebase.google.com/docs/firestore/security/rules-conditions

You can see an example of using firebase rules to access the request time and compare with the target document here: https://fireship.io/snippets/firestore-rules-recipes/#time-based-rules-examples , specifically you could compare the request.time and resource.data.lastDateForSubmission fields.您可以在此处查看使用 firebase 规则访问请求时间并与目标文档进行比较的示例: https://fireship.io/snippets/firestore-rules-recipes/#time-based-rules-examples ,具体可以比较request.timeresource.data.lastDateForSubmission字段。

If you do require changing the variable, then you will have to create a server-side function to do that (eg firebase functions), which you can trigger on a cron schedule (eg every hour, or depending on the types of time intervals you will be specifying)如果您确实需要更改变量,那么您将必须创建一个服务器端 function 来执行此操作(例如 firebase 函数),您可以按 cron 计划(例如每小时或取决于您的时间间隔类型)触发它将指定)

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

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