gpt4 book ai didi

angular - firebase.firestore.FieldValue.serverTimestamp() 不支持时间戳数据类型

转载 作者:行者123 更新时间:2023-12-04 14:45:41 25 4
gpt4 key购买 nike

 async createUser(uid: string, email: string) {
await this.afs.doc<UserProfileModel>(FirestoreDbConstant.USER_PROFILES + `/${uid}`).set({
email,
createdDate: firebase.firestore.FieldValue.serverTimestamp(),
});
}

这个模型有效
export interface UserProfileModel {
email: string;
createdDate?: firebase.firestore.FieldValue;
}

但是为什么我不能使用 Timestamp这里像这样?
import { Timestamp } from '@firebase/firestore-types';

export interface UserProfileModel {
email: string;
createdDate?: Timestamp;
}

然后它给出了这个错误:

(property) UserProfileModel.createdDate?: Timestamp Type 'FieldValue' is missing the following properties from type 'Timestamp': seconds, nanoseconds, toDate, toMillists(2739) user-profile.model.ts(11, 5): The expected type comes from property 'createdDate' which is declared here on type 'UserProfileModel'



这里的问题是我需要使用 toDate()在模板上。但它不适用于 createdDate?: firebase.firestore.FieldValue;
<p>{{invitedEvent.startDatetime.toDate() | amDateFormat: 'MMM DD'}}</p>

最佳答案

正如您从 JavaScript API documentation for serverTimestamp() 中看到的:

serverTimestamp(): FieldValue

Returns a sentinel used with set() or update() to include a server-generated timestamp in the written data.

Returns FieldValue



它不返回时间戳类型。它返回一个 FieldValue,它的值实际上只是一个特殊值的占位符,它告诉服务器生成时间戳。客户端根本不生成这个时间戳,因为我们不能信任客户端设备的时钟。

这意味着您不能真正使用相同的模型来读取和写入使用服务器时间戳的记录。在途中,它将是一个 FieldValue。在出路时(当您查询文档时),它将是一个时间戳。

关于angular - firebase.firestore.FieldValue.serverTimestamp() 不支持时间戳数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com