Angularfire: How to get Firebase server timestamp?

Created on 6 Jun 2016  路  14Comments  路  Source: angular/angularfire

I want to save date to the firebase server with server generated timestamp using Firebase module 'Firebase.ServerValue.TIMESTAMP'.

How can I configure to import Firebase as module. I am using angular-cli and following are the configuration settings.

**System-config.ts**
/** Map relative paths to URLs. */
const map: any = {
    firebase: 'vendor/firebase/lib/firebase-web.js',
    angularfire2: ' vendor/angularfire2'
};
/** User packages configuration.*/
const packages: any = {
      angularfire2: {
        defaultExtension: 'js',
        main: 'angularfire2.js'
      }
};

**angular-cli-build.js**

/* global require, module */
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',

      // below are the AngularFire entries
      'angularfire2/**/*.js',
      'firebase/lib/*.js'      
    ]
  });
};

Most helpful comment

Never mind, I added an extra line to the firebase3 typings file:

declare namespace firebase.database.ServerValue {
var TIMESTAMP: any
}

All 14 comments

You can use firebase.database.ServerValue.TIMESTAMP

See the docs here: https://firebase.google.com/docs/reference/js/firebase.database#.ServerValue

I get a Property 'ServerValue' does not exist on type 'typeof database'.

Never mind, I added an extra line to the firebase3 typings file:

declare namespace firebase.database.ServerValue {
var TIMESTAMP: any
}

using angular-cli (angular 2) you would add majodi's comment to src/typings.d.ts, and then at the top of your component: import * as firebase from 'firebase'.

at least this is what worked for my particular situation as nothing else was working and/or was changing way too many things in way too many locations. above solution is pretty straight forward if using above setup

@epiphanatic @majodi
thank you!! 馃憤 馃憤 馃憤

@Metafili @majodi @epiphanatic I am still having a problem with this.

import * as firebase from 'firebase';
var y = firebase.database.ServerValue.TIMESTAMP;
console.log(y);

I get this:
screen shot 2016-11-11 at 1 46 37 am

Any thought? Thank you !

What problem? That looks exactly like the placeholder for a TIMESTAMP, which we send to the server.

@tonyawad88 just to clarify katowulf's comment, that is exactly what it should be - logging it to the console will just give a placeholder object - this object won't have any value until it is sent to the firebase server in some way (it won't just put a timestamp in a variable like PHP or something would). So for example, if I were to do something like
let dateCreated = firebase.database.ServerValue.TIMESTAMP;
there would be no value until I did something else like
this.af.database.list('/users').push({ uid: uid, dateCreated: dateCreated });
at which point a new user is pushed to the database with the date that the user was created

Ohh I understand now @epiphanatic thank you !

For those who want to avoid all these imports and declarations, you can use this syntax
firebase.database['ServerValue']['TIMESTAMP']

https://github.com/angular/angularfire2/issues/353

If you use lodash in your project, you can use _.get(firebase, 'database.ServerValue.TIMESTAMP') too. (It can be useful if you have a strict linter 馃槈 )

Can i get just server timestamp string

@E-Kom I'm afraid import and declare are still needed as @epiphanatic mentioned

I solved this way:
importations: import { AngularFirestore } from '@angular/fire/firestore';

code:

constructor(private db: AngularFirestore) {}

private getTimestamp(): Object {
    return this.db.firestore['_firebaseApp'].firebase_.firestore.FieldValue.serverTimestamp();
}

Dependencies:

  "@angular/fire": "^5.3.0",
  "firebase": "^7.7.0",
Was this page helpful?
0 / 5 - 0 ratings