在特定时间后更新Firebase实时数据库 [英] Update Firebase Realtime Database after specific time
问题描述
考虑以下存储在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屋!