我需要将时间添加到admin.database.ServerValue.TIMESTAMP中,然后再检索它。但是,当我尝试向添加更多时间时,ServerValue.TIMESTAMP
出现错误:
getTime不是函数
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const ten_secs = 10 * 1000; // 10 seconds
const daily_secs = 24 * 60 * 60 * 1000; // 24 hrs
const weekly_secs = 168 * 60 * 60 * 1000; // 1 week
exports.update = functions.https.onRequest((request, response) => {
const currentTimeStamp = admin.database.ServerValue.TIMESTAMP;
const updatedSecs = new Date(currentTimeStamp.getTime() + ten_secs); // should be saved in the db as milliseconds for later retrieve and calculations
const updatedDay = new Date(currentTimeStamp.getTime() + daily_secs); // should be saved in the db as milliseconds for later retrieve and calculations
const updatedWeek = new Date(currentTimeStamp.getTime() + weekly_secs); // should be saved in the db as milliseconds for later retrieve and calculations
console.log("updatedSecs: " + updatedSecs + " | updatedDay: " + updatedDay + " | updatedWeek: " + updatedWeek);
const ref = admin.database().ref('schedule').child("scheduleId_123").child("my_uid")
ref.once('value', snapshot => {
if (!snapshot.exists()) {
return ref.set({ "updatedSecs": updatedSecs, "updatedDay": updatedDay, "updatedWeek": updatedWeek });
} else {
const retrieved_updatedSecs = snapshot.child("updatedSecs").val();
const retrieved_updatedDay = snapshot.child("updatedDay").val();
const retrieved_updatedWeek = snapshot.child("updatedWeek").val();
const currentTime = Date.now();
// do some calculations with the above values and currentTime.
}
});
}
ServerValue.TIMESTAMP
不是可以进行数学运算的标准整数timstamp值。这是一个令牌或哨兵值,当服务器接收到写操作时,它将在服务器上进行解释。这样便可以获取实际服务器的时间戳值。唯一有意义的使用方式是在写时将其作为孩子的价值。
由于您是在Cloud Functions中运行的,因此实际上您的内存中有一个Google服务器时间戳值-在实际时钟中。Google的所有后端都有同步的时钟,因此它们都是准确的。通常,只有ServerValue.TIMESTAMP
在不确定用户设备的时钟是否准确时,才在客户端应用程序中使用。
在您的情况下,不要使用,而ServerValue.TIMESTAMP
应仅将其Date.now()
作为当前时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句