使用Firebase的服务器端计算

Server Side Calculation using Firebase

本文关键字:计算 服务器端 Firebase 使用      更新时间:2023-09-26

给定开始时间/日期和持续时间,如何进行服务器端计算以确定对象是"finished""in progress"还是"upcoming"

--Show
  --duration: "144"
  --startDate: "2015-11-10"
  --startTime: "14:00"
  --status: "?"

客户端javascript以确定节目是否已经开始:

// if negative, then show hasn't started yet
var time = (-(startdate.getTime() - currentdate.getTime()) / 1000 / 60); 

客户端javascript,用于确定节目是否已完成运行:

// if negative, then show has finished
var timeLeft = channelDuration - timerStartTime;

没有办法在Firebase上运行自己的服务器端代码。参见:

  • 通用Firebase应用程序体系结构
  • Firebase主机自带服务器node.js
  • 如何在Firebase中运行服务器端代码
  • 如何在使用firebase时编写自定义代码(逻辑)

但是可以存储服务器端时间戳,这似乎是您想要做的:

ref.child('Show/startTimestamp').set(Firebase.ServerValue.TIMESTAMP);

然后你可以获得尚未开始的节目:

var shows = ref.child('Shows');
ref.orderByChild('startTimeStamp').startAt(Date.now()).on(...

对于路过的人,我认为现在Firebase允许你通过Cloud Function来完成这项工作。对于这种情况,当数据添加到数据库中时,您可以创建通过其他参数确定状态的status的函数。

请结账

https://firebase.google.com/docs/functions/