Hello, trying to figure out how to get a server timestamp with angularfire2/firestore,
I tried this but it does not work:
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Ticket } from '../../model/interfaces/ticket.interface';
import * as fs from 'firebase/firestore';
@Injectable()
export class FirestoreProvider {
private ticketsCollection:AngularFirestoreCollection<Ticket>;
constructor( private afStore:AngularFirestore )
{
this.ticketsCollection = afStore.collection<Ticket>('Tickets');
}
addTicket(ticket:Ticket){
ticket.createdAt = fs.FieldValue.serverTimestamp(); //<-- This will be null and throw an error.
return this.ticketsCollection.add(ticket);
}
}
Is there any other way to set the server timestamp in a document field?.
Seems to work if you import all of firebase...
```
import * as firebase from 'firebase';
...
ticket.createdAt = firebase.firestore.FieldValue.serverTimestamp()
````
maaan.. I'm getting mad at this.
`private clientsCollection: AngularFirestoreCollection
constructor(private db: AngularFirestore) {
}
getAll() {
return this.db.collection<ClientModel>('clients');
}
add(client: ClientModel) {
this.clientsCollection = this.db.collection<ClientModel>('clients');
return this.clientsCollection.add(client);
}`
Look at this, it is a simple add ObjectModel to collection, but when I try to execute it I receive this:
Function CollectionReference.add() requires its first argument to be of type object, but it was: a custom ClientModel object
It is almost the same as your piece of code, but it does not work :/
@eduardowickertg I don't think you issue is related to this issue.
@eduardowickertg I have the same problem and I'm getting mad, @Toxicable Do you have a simple solution?
@Wanted92 @eduardowickertg I explained what's going on here https://github.com/angular/angularfire2/issues/1215#issuecomment-334577913
@stubborncoder does your add method work? Without the the time stamp? I followed the docs and didnât use the second line where you use âreturn xxxxâ. Maybe thatâs why my add method is broken.
@stubborncoder I think I found something that you could try for timestamps. It works for me wonderfully!
import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import * as firebase from 'firebase';
import { Observable } from 'rxjs/Observable';
export interface Announcement { id: string; title: string; body: string, createdAt: Date }
@Component({
selector: 'app-announcement-form',
templateUrl: './announcement-form.component.html',
styleUrls: ['./announcement-form.component.css']
})
export class AnnouncementFormComponent implements OnInit {
private announcementCollection: AngularFirestoreCollection<Announcement>;
announcements: Observable<Announcement[]>;
constructor(private afs: AngularFirestore) {
this.announcementCollection = afs.collection<Announcement>('announcements');
this.announcements = this.announcementCollection.valueChanges();
}
ngOnInit() {
console.log("Hello");
}
addAnnouncement(title: string, body: string){
const id = this.afs.createId();
const createdAt = new Date();
console.log(createdAt);
const announcement: Announcement = {id: id, title: title, body:body, createdAt: createdAt };
console.log(announcement);
this.announcementCollection.add(announcement);
}
}
@callen5914 yes my method works without the timestamp line, your suggestion works fine but bear in mind that you are saving the client's date value, and I would like that field to be more reliable to avoid messing with ordering that collection for example.
With new Date() you will set the date to whatever date you user has in her computer...
Sorry I loged as another user, I'm still the original poster.
I'm also having this issue as @stubborncoder. I'm working in Ionic 3 and I've imported angularfire2 and the Firestore module but when I call
````
import { AngularFirestore } from 'angularfire2/firestore';
import { Injectable } from '@angular/core';
@Injectable()
export class FirebaseService {
constructor(private db: AngularFirestore) { }
addUser(user) {
this.db.collection("users").doc(user.id).set({
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
DoB: user.dateOfBirth,
created_at: this.db.firestore.FieldValue.serverTimestamp(),
updated_at: this.db.firestore.FieldValue.serverTimestamp()
}).then(res => {
console.log("User created. ")
}).catch(error => {
console.error("Error adding user: ", error);
});
}
I get property "FieldValue" does not exist on type Firestore when I call:
this.db.firestore.FieldValue.serverTimestamp(),
````
As @sionfletcher stated some days ago, you have to use the whole firebase thingy, the service approach here does not work for some reason:
import * as firebase from âfirebaseâ;
ticket.createdAt = firebase.firestore.FieldValue.serverTimestamp()
Just a small improvement for going to production with Firestore.
If you don't want to see warning development messages.
import * as firebase from 'firebase/app';
import 'firebase/firestore';
ticket.createdAt = firebase.firestore.FieldValue.serverTimestamp();
import * as firebase from 'firebase/app';
still get the warning
It looks like you're using the development build of the Firebase JS SDK.
even cleaner:
import { firestore } from 'firebase/app';
ticket.createdAt = firestore.FieldValue.serverTimestamp();
This is useful, but it's not at all easy to test.
Most helpful comment
Seems to work if you import all of firebase...
```
import * as firebase from 'firebase';
...
ticket.createdAt = firebase.firestore.FieldValue.serverTimestamp()
````