Has angularfire2 a way to update data with the current server timestamp?
Analogous to Firebase.ServerValue.TIMESTAMP of the 2.4.2 version?
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:
"firebase": "file:node_modules/firebase/firebase.d.ts" to my globalDependencies.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.
Most helpful comment