Angularfire: Server Timestamp

Created on 13 Jul 2016  路  17Comments  路  Source: angular/angularfire

Has angularfire2 a way to update data with the current server timestamp?
Analogous to Firebase.ServerValue.TIMESTAMP of the 2.4.2 version?

Most helpful comment

`this.timestamp = firebase.database['ServerValue']['TIMESTAMP'];`

All 17 comments

It's still the same behavior just a new API on the firebase object. We don't need to natively support it in AngularFire2 though.

firebase.database.ServerValue.TIMESTAMP

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

Why do we not need to support this? Because we can manually add it ourselves - like so? https://github.com/angular/angularfire2/issues/211#issuecomment-230244521

@dsummersl It should work without the ambient declaration. Is that not the case?

It doesn't seem to be, but maybe I'm not doing it correctly (very new to typescript). I tried something like this:

export class PlanService {
  constructor(private af: AngularFire) { }
  submitPlan(plan: Plan): Promise<string> {
    return new Promise<string>((resolve, reject) => {
      this.af.auth.subscribe(auth => {
        let designs = this.af.database.list('/designs');
        let new_key = designs.push({
          auth.uid,
          name: plan.name,
          created: this.af.database.ServerValue.TIMESTAMP
        });
        ...
        ... resolve or reject ...
        ...
      });
    });
  }
}

The compiler gives me an error like:

The Broccoli Plugin: [BroccoliTypeScriptCompiler] failed with:
Error: Typescript found the following errors:
  ...6vw11KGr.tmp/0/src/app/plan.service.ts (54, 37): Property 'ServerValue' does not exist on type 'AngularFireDatabase'.
    at BroccoliTypeScriptCompiler._doIncrementalBuild (node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:120:19)
    at BroccoliTypeScriptCompiler.build (node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:43:10)

What version of AngularFire, Angular 2, and TypeScript are you using?

angular-2.0.0-rc.5, angularfire2-2.0.0-beta.4, typescript-1.8.10 -- checking on how outdated that all is...

@dsummersl update to typescript 2

Yep, what @danielgek said!

hrm, upgraded to typescript 2.0.2 and I get the same error still. Do I need to be somehow importing from firebase rather than angularfire2 to get a reference to TIMESTAMP?

You need to import Firebase if you want to use the timestamp. Make sure you're on the latest Firebase SDK 3.3 right now.

Ok! I got it to work. For future reference, this https://github.com/angular/angularfire2/issues/211#issuecomment-230244521 is the essence of the solution:

  1. I'm using angular-cli. Add it to angular-cli-build.js, and src/system-config.js (I think thats part of the install anyway).
  2. Add typings to typings.json. I addded "firebase": "file:node_modules/firebase/firebase.d.ts" to my globalDependencies.
  3. Add TIMESTAMP support by adding the block in the comment above ti src/typings.d.ts
  4. Import firebase as import * as firebase from 'firebase'; where you want to use TIMESTAMP, and then refernce it as firebase.database.ServerValue.TIMESTAMP.

Thanks so much for the help.

`this.timestamp = firebase.database['ServerValue']['TIMESTAMP'];`

@andlcool thanks it worked for me

@andlcool i try

this.dateStamp = firebase.database['ServerValue']['TIMESTAMP'] ;

this.dateConvert =  new Date(this.dateStamp);

but the answer is Invalid Date

@yuber1792 you are doing it the wrong way...
firebase.database['ServerValue']['TIMESTAMP'] is a value understandable by firebase only. when you put this value in a variable and do a set or update on firebase this value will be replaced by a server timestamp corresponding to the moment you did your set/update.
You cannot use this value as a javascript date since it is not.

I get this in console

Object {.sv: "timestamp"}
.sv
:
"timestamp"
__proto__
:
Object

I get that it's undefined.

I'm doing it like:

angularFireDb.database['ServerValue']['TIMESTAMP']

AngularFireDb is of type AngularFireDatabase.

Was this page helpful?
0 / 5 - 0 ratings