亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何向 firebase.database.ServerValue.TIMESTAMP 添加時間

如何向 firebase.database.ServerValue.TIMESTAMP 添加時間

泛舟湖上清波郎朗 2023-07-20 17:11:22
我需要向 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 secondsconst daily_secs = 24 * 60 * 60 * 1000; // 24 hrsconst weekly_secs = 168 * 60 * 60 * 1000; // 1 weekexports.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.? ? ? ? }? ? });}
查看完整描述

3 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

ServerValue.TIMESTAMP不是可以進行數學計算的標準整數時間戳值。它是一個令牌或哨兵值,當服務器收到寫入時會在服務器上進行解釋。這就是它能夠獲取實際服務器的時間戳值的方式。唯一有意義的使用方法是作為寫入時的子值。

由于您在 Cloud Functions 中運行,因此內存中實際上有一個 Google 服務器時間戳值 - 在實際時鐘中。谷歌的所有后端都有同步時鐘,因此它們都是準確的。ServerValue.TIMESTAMP當您無法確定用戶的設備具有準確的時鐘時,您通常僅在客戶端應用程序中使用。

在您的情況下,ServerValue.TIMESTAMP您應該簡單地采用Date.now()當前時間,而不是使用 。


查看完整回答
反對 回復 2023-07-20
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

currentTimeStamp 后面需要一個右括號。

const updatedSecs = new Date(currentTimeStamp).getTime() + ten_secs;


查看完整回答
反對 回復 2023-07-20
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

我遇到了另一個getTime is not a function仍然不斷出現的問題。我不得不改用.valueOf()。


這是更新后的代碼:


const currentTimeStamp = Date.now(); // DougStevenson answer


const updatedSecs = currentTimeStamp.valueOf() + ten_secs;


const updatedDay = currentTimeStamp.valueOf() + daily_secs;


const updatedWeek = currentTimeStamp.valueOf() + weekly_secs;


查看完整回答
反對 回復 2023-07-20
  • 3 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號