【发布时间】:2018-12-06 13:07:34
【问题描述】:
我得到了错误。
无法读取未定义的属性“toDate”。没有
toDate() | Date
我明白了
时间戳(秒=1545109200,纳秒=0)
我引用了这个Angular 6 firebase snapshot returns undefined
但是,将时间戳转换为可读的日期字符串并不是特定的。 期望的输出是 2018 年 12 月 6 日。沿着这些思路。
schedule-list.component.html
<div class="container ">
<div class="row">
<div *ngFor="let appoint of list" class="col-md-8 myCenter mt-2">
<!-- the bottom code should work if items within list exist. -->
<div class="card">
<div class="card-header">
Title: {{appoint.name}}
</div>
<div class="card-body">
<p>{{appoint.appointment_date.toDate() | date}}</p>
</div>
</div>
</div>
</div>
</div>
schedule.model.ts
export class Schedule {
name:string;
appointment_date:string;
}
Pipe.ts
import {formatDate} from '@angular/common';
import {Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core';
import {firestore} from 'firebase/app';
import Timestamp = firestore.Timestamp;
@Pipe({
name: 'fireStoreDatePipe'
})
export class FireStoreDatePipePipe implements PipeTransform {
constructor(@Inject(LOCALE_ID) private locale: string) {
}
transform(timestamp: Timestamp, format?: string): string {
return formatDate(timestamp.toDate(), format || 'medium', this.locale);
}
}
Schedule-list.component.ts
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { ScheduleService } from '../schedule.service';
import { Schedule } from '../models/schedule.model';
import {FireStoreDatePipePipe} from '../fire-store-date-pipe.pipe';
import { Timestamp } from '@firebase/firestore-types';
@Component({
selector: 'app-schedule-list',
templateUrl: './schedule-list.component.html',
styleUrls: ['./schedule-list.component.css']
})
export class ScheduleListComponent implements OnInit {
list:Schedule[];
constructor(private firestore: AngularFirestore, private service: ScheduleService ) { }
ngOnInit() {
this.service.getAppointments().subscribe(actionArray =>{
this.list = actionArray.map(item =>{
return{
...item.payload.doc.data()
} as Schedule;
})
});
}
getSchedules(){
return this.firestore.collection('schedules').snapshotChanges();
}
}
【问题讨论】:
-
没有管道会发生什么。
{{appoint.appointment_date.toDate() }} -
它已经在代码上方的帖子中解释过。
-
只是管道。保持
toDate() -
那不是我想要的。我需要获取客户在 datepicker 上选择的日期。所以我需要得到约会日期
标签: angular typescript firebase google-cloud-firestore