简体   繁体   中英

How to add time to a firebase.database.ServerValue.TIMESTAMP

I need to add time to a admin.database.ServerValue.TIMESTAMP and later retrieve it. But when I try to add additional time to the ServerValue.TIMESTAMP I get an error:

getTime is not a function

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 is not a standard integer timstamp value that you can do math on. It's a token, or sentinel value, that gets interpreted on the server when it receives the write. That's how it's able to take the actual server's timestamp value. The only way it makes sense to use is as a child's value at the time of a write.

Since you are running in Cloud Functions, you actually have a Google server timestamp value right there in memory - in the actual clock. All of Googles backends have synchronized clocks, so they are all accurate. You typically only use ServerValue.TIMESTAMP in a client app when you can't be certain that the user's device has an accurate clock.

In your case, instead of using ServerValue.TIMESTAMP , you should simply take Date.now() as the current time.

currentTimeStamp 后需要一个右括号。

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

@DougStevenson's answer worked but I ran into another problem where getTime is not a function still kept appearing. I had to use .valueOf() instead.

Here is the updated code:

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

const updatedSecs = currentTimeStamp.valueOf() + ten_secs;

const updatedDay = currentTimeStamp.valueOf() + daily_secs;

const updatedWeek = currentTimeStamp.valueOf() + weekly_secs;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM