问题
Consider the following database in JSON format stored in firebase -
"root":
{
"survey":
{
"1":
{
"survey_question":"Who is a better player?",
"option1":"Ronaldo",
"option2":"Messi",
"time_posted":7854123265,
"visibility":1
},
"2":
{
"survey_question":"Who is a better singer?",
"option1":"Ed Sheeran",
"option2":"Chris Martin",
"time_posted":9865321245,
"visibility":1
}
}
}
I can write a Firebase cloud function which is triggered when a new survey is created.
But what should I do to update the visibility to 0
after 24 hours from time_posted
?
Edit: I checked out the solution for this question and followed the instructions-
- Wrote the function, in
index.js
file, which I want to be triggered and deployed it successfully. - Created a cron job at cron-job.org with the below address of the function-
https://us-central1-<PROJECT-ID>.cloudfunctions.net/<FUNCTION-NAME>?key=<PROJECT-KEY>
The cron job execution failed with the following error message-Error: Forbidden
Your client does not have permission to get URL /<FUNCTION-NAME>?key=<PROJECT-KEY> from this server.
回答1:
It might be easier to do a time diff using time_posted
to check for Date.now() - time_posted <= 24 * 60 * 60 * 1000
(ensure that it is within one day of being posted).
Or, on a child_added
event, you could do a setTimeout
that calls back after 24 hours and updates the visibility
flag, but that seems kind of messy.
来源:https://stackoverflow.com/questions/49658728/update-firebase-realtime-database-after-specific-time