我正在尝试加载一个字段,该字段作为时间戳存储在firestore中,并填充一个使用日期格式的“mat-datepicker”表单控件。
在到达角度材质日期选择器控件之前,有没有办法修改“道布”数据的格式?日期选择器不喜欢时间戳格式,并以显而易见的原因拒绝显示它。
下面是我尝试缩短现有代码减去任何转换尝试,因为我被卡住了!它甚至可能不起作用!不过,完整的代码是这样做的。
基本上,使用传入的路径字符串来创建Firestore文档的实例,然后将值修补到myForm控件中。
我知道firebase.firestore.Timestamp有一个toDate()选项,但我是一个新手,在值存储到控件之前,我需要一些关于如何将时间戳转换为日期的提示?
// Firestore Document
private fireDoc: AngularFirestoreDocument;
@Input() path: string;
myForm: FormGroup;
constructor(private fb: FormBuilder, private afs: AngularFirestore) { }
ngOnInit() {
this.myForm = this.fb.group({
firstName: new FormControl(''),
middleName: new FormControl(''),
lastName: new FormControl(''),
dob: new FormControl(),
});
this.loadDoc();
}
loadDoc() {
this.fireDoc = this.afs.doc(this.path);
this.fireDoc
.valueChanges()
.pipe(
tap(doc => {
if (doc) {
this.myForm.patchValue(doc);
}
}),
take(1)
)
.subscribe();
}
}发布于 2020-05-17 10:28:26
我在angularfirebase.slack.com上问了这个问题,并得到了这个解决方案。
pipe(
map(({dob, ...data}) => {
return {
...data,
dob: dob.toDate(),
};
}),点击(...
在我的场景中,我在firestore中有5个时间戳,并且正在碰壁。上面的代码对我来说很有效。
https://stackoverflow.com/questions/55195093
复制相似问题