Firebase.ServerValue.TIMESTAMP returns object literal (JSON) instead of number
See original GitHub issueThe 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:
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.
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
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.
@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.