简体   繁体   中英

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

I have a Firestore Document which has these 2 properties:

isActive: true lastDateForSubmission: a timestamp.

I am using the isActive property to enable or disable upload in my React Application. Currently the lastDateForSubmission is just for display.

I dont have any backend servers. I am using plain React and Firebase.

I need to automatically set the isActive property to False after the time in the LastDateForSubmission property.

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.

You can schedule tasks and update your database. Look at https://firebase.google.com/docs/functions/schedule-functions

Firestore is a database so it cannot execute code by itself. 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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