使用 Firebase 的服务器端计算

Server Side Calculation using Firebase

给定起始 time/date 和持续时间,我如何进行服务器端计算以确定对象是 "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 上 运行 您自己的服务器端代码。参见:

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

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/