【问题标题】:How to get firestore timestamp如何获取firestore时间戳
【发布时间】:2018-10-05 08:24:02
【问题描述】:

我正在尝试获取我在 Firestore 中创建的文档的时间戳,但我得到的是:

myService.ts

getDomiciliarios() {
this.domiciliarios = this.afs.collection('domiciliarios').snapshotChanges().map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data() as Domiciliario;
    const id = a.payload.doc.id;
    const date = firebase.firestore.FieldValue.serverTimestamp();
    return { id, ...data, date };
  });
});

return this.domiciliarios;
}

myComponent.ts

ngOnInit() {
const domiciliarios = this.fs.getDomiciliarios()
  .subscribe(data => this.dataSource.data = data, date => date);
}

myComponent.html

<ng-container matColumnDef="fecha">
  <mat-header-cell *matHeaderCellDef mat-sort-header> Fecha </mat-header-cell>
  <mat-cell *matCellDef="let domiciliario"> {{ domiciliario.date }} </mat-cell>
</ng-container>

如何打印那个时间戳,我应该之前创建过它吗?

【问题讨论】:

  • 尝试使用 date 管道作为&lt;mat-cell *matCellDef="let domiciliario"&gt; {{ domiciliario.date | date:'dd-MMM-yyyy' }} &lt;/mat-cell&gt;
  • @GuruprasadRao 我收到以下错误Error: InvalidPipeArgument: '[object Object]' for pipe 'DatePipe'
  • 在喂给const date=之前尝试new Date(timestampvalue)

标签: angular typescript firebase google-cloud-firestore angularfire2


【解决方案1】:

如果您想要firebase.firestore.FieldValue.serverTimestamp() 的日期值,您可以使用.toDate()。 见FireStore Timesteamp。在您的情况下,您的模板中将是 domiciliario.date.toDate()

【讨论】:

    【解决方案2】:

    我认为您使用 FieldValue.serverTimestamp() 的方式是错误的:正如文档所述,firebase.firestore.FieldValue 方法返回“可以在 使用 set() 或 update() 编写文档字段时使用的值 ”。 (见https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue

    您在读取数据时正在使用serverTimestamp() 方法。

    您应该在在数据库中创建记录时使用它,正如您在问题末尾提到的那样。

    编辑: 执行以下操作:

    const timestamp = firebase.firestore.FieldValue.serverTimestamp();
    docRef.update({ updatedAt: timestamp });
    

    然后你可以像这样查询

    collectionRef.orderBy('updatedAt').get()
        .then(snapshot => {
            snapshot.forEach(doc => {
                ...
            });
        })
        .catch(err => {
            console.log('Error getting documents', err);
        });
    

    上面的代码是纯 JavaScript,你可以把它改成 angular 和 type 脚本,但原理是一样的。

    【讨论】:

    • 这个时间戳不是和ID一样自动生成的吗?如果没有,那么如何在 Firestore 中添加该时间戳?
    • 谢谢我用firebase.firestore.FieldValue.serverTimestamp ();添加了一个变量,这在Firestore中添加了一个带有日期的字段。我会用这个来回应的。
    • @PacoZevallos 酷! :-) 如果您现在按此值查询 ordered,您应该得到您正在寻找的内容
    • 我应该提到new Date () 也适用于我,我不明白创建时间戳时与firebase.firestore.FieldValue.serverTimestamp () 有什么区别,但它也适用于我。
    • @PacoZevallos new Date() 是您的客户端计算机(用户浏览器)的当前时间戳,而 firebase.firestore.FieldValue.serverTimestamp () 是 Firebase 服务器的时间戳
    【解决方案3】:

    如果您希望当前日期作为时间戳,您可以使用以下内容

    对于 Firebase 函数

    import admin = require('firebase-admin');
    
    const todayAsTimestamp = admin.firestore.Timestamp.now()
    

    对于本地项目

    import { Timestamp } from '@google-cloud/firestore';
    
    const myTimestampAsDate = Timestamp.now()
    

    【讨论】:

    • 返回用户的本地日期时间。如果用户的日期/时间错误,您将得到错误的日期/时间
    【解决方案4】:

    感谢@Renaud,他强调了我在哪里实现时间戳,它应该是在 Firestore 中创建记录时。就我而言,我正在使用 formGroup,那么代码将如下所示:

    forma: FormGroup;
    
    constructor( fb: FormBuilder) { 
      this.forma = fb.group ({
        field1: [ ''],
        field2: [ ''],
        ...
        myDate: [ firebase.firestore.FieldValue.serverTimestamp() ],
      });
    }
    

    此替代方案也有效,但请记住这是客户端计算机(用户浏览器)的当前时间。

    forma: FormGroup;
    
    constructor( fb: FormBuilder) { 
      this.forma = fb.group ({
        field1: [ ''],
        field2: [ ''],
        ...
        myDate: [ new Date() ],
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-11
      • 2021-05-19
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多