在特定时间后更新Firebase实时数据库 [英] Update Firebase Realtime Database after specific time

本文介绍了在特定时间后更新Firebase实时数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下存储在firebase中的JSON格式的数据库-

"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
        }
    }
}

我可以编写一个Firebase云功能,该功能在创建新调查时触发.
但是,从time_posted开始24小时后,我应该怎么做才能将可见性更新为0?

编辑:我签出了的解决方案这个问题,并按照说明进行操作-

  • index.js文件中编写了要触发并成功部署的功能.
  • 通过 cron-job.org 创建了一项cron作业,并使用以下函数地址-
    https://us-central1-<PROJECT-ID>.cloudfunctions.net/<FUNCTION-NAME>?key=<PROJECT-KEY>

cron作业执行失败,并显示以下错误消息-
Error: Forbidden Your client does not have permission to get URL /<FUNCTION-NAME>?key=<PROJECT-KEY> from this server.

解决方案

使用time_posted进行时间差异检查Date.now() - time_posted <= 24 * 60 * 60 * 1000(确保它在发布后的一天之内)可能会更容易. /p>

或者,在child_added事件上,您可以执行setTimeout,该setTimeout在24小时后回调并更新visibility标志,但这似乎有些混乱.

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.

解决方案

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.

这篇关于在特定时间后更新Firebase实时数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆