Angular / Firebase / Typescript Interfaces - 如何使用 toDate() 管理 firebase.firestore.FieldValue 的接口类型
Angular / Firebase / Typescript Interfaces - How to manage interface type of firebase.firestore.FieldValue with toDate()
我有一个界面,其中日期设置为使用 firestores 时间戳。
export interface Album{
album_name: string,
album_date: firebase.firestore.FieldValue;
}
如果我想添加一个新项目,这完全没问题
this.albumsCollection.add({
album_name: "My Album",
album_date: firebase.firestore.FieldValue.serverTimestamp(),
})
正在从 firestore 获取相册文件
但是,从 firestore 检索相册 Document 后,如果我排除 album_date
,下面的代码将正常工作。但是,我正在尝试使用 toDate
以便它出现在 Angular Material 日期选择器
中
public getAlbum(): Observable<Album> {
this.itemDoc = this.afs.doc<Album>(`albums/1`);
return this.itemDoc.snapshotChanges()
.pipe(map(collectionDoc => {
const data = collectionDoc.payload.data();
return {
...data,
album_date: (data.album_date as firebase.firestore.Timestamp).toDate(),
} as Inspection;
}),
shareReplay()
);
}
在线收到的错误:album_date: (data.album_date as firebase.firestore.Timestamp).toDate(),
如下:
Conversion of type '{ album_Date: Date; }' to type ‘Album` may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Types of property ‘album’_date are incompatible.
Property 'isEqual' is missing in type 'Date' but required in type 'FieldValue'.
当接口也为 firestore 配置时,使用 'toDate' 返回的 firestore 时间戳的最佳管理方式是什么?
至少有两种方法可以解决这个问题。由于寻求意见的问题与 Stack Overflow 无关,您可以自行决定哪一个最适合您的情况。
您可以有两种不同的相册界面,一种用于阅读,一种用于写作。写的可以用FieldValue类型,读的可以用Timestamp类型
您也可以使用 TypeScript's union type 表示 album_date 可以是 FieldValue 或 Timestamp。
album_date: firebase.firestore.FieldValue | firebase.firestore.Timestamp
这应该允许您根据需要投射单个字段。
我有一个界面,其中日期设置为使用 firestores 时间戳。
export interface Album{
album_name: string,
album_date: firebase.firestore.FieldValue;
}
如果我想添加一个新项目,这完全没问题
this.albumsCollection.add({
album_name: "My Album",
album_date: firebase.firestore.FieldValue.serverTimestamp(),
})
正在从 firestore 获取相册文件
但是,从 firestore 检索相册 Document 后,如果我排除 album_date
,下面的代码将正常工作。但是,我正在尝试使用 toDate
以便它出现在 Angular Material 日期选择器
public getAlbum(): Observable<Album> {
this.itemDoc = this.afs.doc<Album>(`albums/1`);
return this.itemDoc.snapshotChanges()
.pipe(map(collectionDoc => {
const data = collectionDoc.payload.data();
return {
...data,
album_date: (data.album_date as firebase.firestore.Timestamp).toDate(),
} as Inspection;
}),
shareReplay()
);
}
在线收到的错误:album_date: (data.album_date as firebase.firestore.Timestamp).toDate(),
如下:
Conversion of type '{ album_Date: Date; }' to type ‘Album` may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. Types of property ‘album’_date are incompatible. Property 'isEqual' is missing in type 'Date' but required in type 'FieldValue'.
当接口也为 firestore 配置时,使用 'toDate' 返回的 firestore 时间戳的最佳管理方式是什么?
至少有两种方法可以解决这个问题。由于寻求意见的问题与 Stack Overflow 无关,您可以自行决定哪一个最适合您的情况。
您可以有两种不同的相册界面,一种用于阅读,一种用于写作。写的可以用FieldValue类型,读的可以用Timestamp类型
您也可以使用 TypeScript's union type 表示 album_date 可以是 FieldValue 或 Timestamp。
album_date: firebase.firestore.FieldValue | firebase.firestore.Timestamp
这应该允许您根据需要投射单个字段。