Update Firebase Realtime Database after specific time [duplicate]

心已入冬 提交于 2020-08-26 12:48:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!