ravi
ravi

Reputation: 1088

How to get Firebase server timestamp?

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 in angular2 project.

I am using Angular2, angular-cli, angularFire2 etc. 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'      
    ]
  });
};

Upvotes: 1

Views: 4993

Answers (2)

John
John

Reputation: 3535

For bypass way, you can use {'.sv': 'timestamp'}

db.object('path').set({ timestamp: {'.sv': 'timestamp'}}) // angular

firebase.database.ServerValue.TIMESTAMP == {'.sv': 'timestamp'}

Upvotes: 2

Jason Simpson
Jason Simpson

Reputation: 4952

Using Angularfire2, Angular 2, and angular-cli, the following solution worked for me.

First, make sure to import firebase with:

import * as firebase from 'firebase';

Next, use the old way of getting the timestamp, for example:

let dateNow = firebase.database.ServerValue.TIMESTAMP;
console.log(dateNow);

Finally, open src/typings.d.ts and add:

declare namespace firebase.database.ServerValue {
  let TIMESTAMP: any;
}

The last part was what did the trick for me and was different from most solutions floating around that aren't using the OPs specific setup.

Upvotes: 7

Related Questions