firechat icon indicating copy to clipboard operation
firechat copied to clipboard

Firebase.ServerValue.TIMESTAMP returns object literal (JSON) instead of number

Open axing opened this issue 13 years ago • 10 comments

The global variable Firebase.ServerValue.TIMESTAMP returns an object literal ({.sv: "timestamp"}) that make both arithmetic and logical operation not possible to work.

Logical: https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L214

if (notification.notificationType !== 'suspension' || notification.data.suspendedUntil < Firebase.ServerValue.TIMESTAMP) {

notification.data.suspendedUntil < Firebase.ServerValue.TIMESTAMP will always be false. (Note: notification.data.suspendedUntil will return as a string, see next error below).

Arithmetic: https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L457

suspendedUntil = Firebase.ServerValue.TIMESTAMP + 1000*timeLengthSeconds;

Let's say timeLengthSeconds is 10, the result of suspendedUntil will be "[object Object]10000". Screenshot from my Firebase Forge: screen shot 2013-08-01 at 4 56 55 pm

I haven't checked for these line codes below, but they seem work fine: https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L295 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L391 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L406 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L441

Thanks.

axing avatar Aug 01 '13 09:08 axing

Great catch - I'll get a fix on it right away.

robertdimarco avatar Aug 01 '13 16:08 robertdimarco

Is this fixed now? I'm still getting {.sv: "timestamp"} when retrieving a date created by Firebase.ServerValue.TIMESTAMP.

oburakevych avatar Aug 12 '13 01:08 oburakevych

Fixed! Closing out.

robertdimarco avatar Aug 14 '13 16:08 robertdimarco

So you just replaced ServerValue.TIMESTAMP with new Date().getTime()?

What if I want to get the actual Date from the object's property saved as ServerValue.TIMESTAMP:

            $scope.authUserRef.transaction(function(user) {
                user.SERVER_TIME = Firebase.ServerValue.TIMESTAMP;
                return user;
            }, function(error, committed, snapshot) {
                if (error) {
                    console.wrror("Error getting the SERVER_TIME");
                }

                if (committed) {
                    var serv = snapshot.val();
                    var serverDate = new Date(snapshot.val().SERVER_TIME * 1000);
                    console.log("Server date: " + serverDate);
                }
            });

snapshot.val().SERVER_TIME = {.sv: "timestamp"}

How do I get the actual Date? Or maybe there is another way to get Firebase server's time?

oburakevych avatar Aug 16 '13 05:08 oburakevych

@oburakevych When you pass around Firebase.ServerValue.TIMESTAMP, the { '.sv': 'timestamp' } value is used as a placeholder until actually written to Firebase via .set(), .push(), etc.

When one of those methods is called with the TIMESTAMP in it, the client will immediately fire any local events for listeners on that data using the client's local timestamp offset by the difference between client and server time as determined when you first connected to Firebase. This value is approximate, and becomes updated when the actual server-side write in Firebase completes.

To get the actual server time from Firebase for a write of some kind, you would first write to Firebase (i.e. ref.child('updated_at').set(Firebase.ServerValue.TIMESTAMP); and have listeners for that data (ref.on('value', function(snapshot) { // snapshot.val().updated_at });). There is no way to get the server time synchronously from Firebase, but the local time + server offset time (provided immediately whenever you write to Firebase using the ServerValue) is a good approximation.

robertdimarco avatar Aug 16 '13 16:08 robertdimarco

Can't we get the timestamp value directly like in android instead of getting { '.sv': 'timestamp' }. Or can we parse it someway to get value? I am also stuck on this.

Tekrajs avatar Sep 11 '17 03:09 Tekrajs

As Rob mentioned, you use server offset time. See Clock Skew in the docs for more details.

katowulf avatar Sep 11 '17 18:09 katowulf

This issue is back: seeing the same value when saving: {.sv: "timestamp"}, for code: data.updated_on = firebase.database.ServerValue.TIMESTAMP;

mikeglowacki avatar Dec 14 '17 11:12 mikeglowacki

The firebase.database.ServerValue.TIMESTAMP value cannot be accessed directly. It is only resolved once it is used in a write, i.e. ref.set(...) or ref.update(...), in which case any functions reacting to changes at those references will be invoked with the replaced value.

robertdimarco avatar Dec 14 '17 19:12 robertdimarco

@oburakevych When you pass around Firebase.ServerValue.TIMESTAMP, the { '.sv': 'timestamp' } value is used as a placeholder until actually written to Firebase via .set(), .push(), etc.

When one of those methods is called with the TIMESTAMP in it, the client will immediately fire any local events for listeners on that data using the client's local timestamp offset by the difference between client and server time as determined when you first connected to Firebase. This value is approximate, and becomes updated when the actual server-side write in Firebase completes.

To get the actual server time from Firebase for a write of some kind, you would first write to Firebase (i.e. ref.child('updated_at').set(Firebase.ServerValue.TIMESTAMP); and have listeners for that data (ref.on('value', function(snapshot) { // snapshot.val().updated_at });). There is no way to get the server time synchronously from Firebase, but the local time + server offset time (provided immediately whenever you write to Firebase using the ServerValue) is a good approximation.

@robertdimarco Is there any way I can get the server time offset without writing anything to firebase?

lahiruchandima avatar Jan 24 '20 03:01 lahiruchandima