简体   繁体   English

firebase.firestore.FieldValue.serverTimestamp() 不支持 Timestamp 数据类型

[英]firebase.firestore.FieldValue.serverTimestamp() doesn't support Timestamp data type

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

This model works这个 model 工作

export interface UserProfileModel {
    email: string;
    createdDate?: firebase.firestore.FieldValue;
}

But Why I cannot use Timestamp here like so?但是为什么我不能像这样在这里使用Timestamp呢?

import { Timestamp } from '@firebase/firestore-types';

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

Then it gives this error:然后它给出了这个错误:

(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' (属性)UserProfileModel.createdDate?:时间戳类型“FieldValue”缺少“时间戳”类型的以下属性:秒、纳秒、toDate、toMillists(2739) user-profile.model.ts(11, 5):预期类型来自在类型“UserProfileModel”上声明的属性“createdDate”

The problem here is I need to use toDate() on the template.这里的问题是我需要在模板上使用toDate() But it doesn't work with the createdDate?: firebase.firestore.FieldValue;但它不适用于createdDate?: firebase.firestore.FieldValue;

<p>{{invitedEvent.startDatetime.toDate() | amDateFormat: 'MMM DD'}}</p>

As you can see from the JavaScript API documentation for serverTimestamp() :正如您从serverTimestamp() 的 JavaScript API 文档中看到的那样:

serverTimestamp(): FieldValue服务器时间戳():字段值

Returns a sentinel used with set() or update() to include a server-generated timestamp in the written data.返回与 set() 或 update() 一起使用的标记,以在写入的数据中包含服务器生成的时间戳。

Returns FieldValue返回字段值

It doesn't return a Timestamp type.它不返回 Timestamp 类型。 It returns a FieldValue, whose value is actually just a placeholder for a special value that tell the server to generate the timestamp.它返回一个 FieldValue,它的值实际上只是一个特殊值的占位符,它告诉服务器生成时间戳。 The client doesn't generate this timestamp at all, since we can't trust the client device's clock.客户端根本不生成这个时间戳,因为我们不能信任客户端设备的时钟。

This means you can't really use the same model for reading and writing records that use server timestamps.这意味着您不能真正使用相同的 model 来读取和写入使用服务器时间戳的记录。 On the way in, it will be a FieldValue.在进入的过程中,它将是一个 FieldValue。 On the way out (when you query the document), it will be a Timestamp.在出路时(当您查询文档时),它将是一个时间戳。

This might be better than 'any' or duplicating models:这可能比“任何”或复制模型更好:

lastModified: Timestamp | FieldValue;

Another option for this is to define your own interface that partially extends both Timestamp and FieldValue另一种选择是定义您自己的接口,该接口部分扩展 Timestamp 和 FieldValue

    export interface MyTimestamp extends Partial<FieldValue>, Partial<Timestamp>
    {
      isEqual: (other: any) => boolean;
      valueOf: () => any;
    }

Then you can set your timestamp props to:然后您可以将时间戳道具设置为:

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

You'll then get proper types for both writing and reading to firestore然后,您将获得用于写入和读取到 Firestore 的正确类型

The one caveat is that all props and functions are now optional so if you want to call toDate() on Timestamp for example you'll need to use ?一个警告是所有道具和功能现在都是可选的,所以如果你想在 Timestamp 上调用toDate()例如你需要使用? user.createdDate?.toDate?.()

Writing:写作:

user.createdDate = FieldValue.serverTimestamp();

Reading:阅读:

user.createdDate?.toDate?.();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可以在 Firebase 函数中使用 firebase-admin.firebase.Timestamp 而不是 FieldValue.serverTimestamp() 吗? - Can you use firebase-admin.firebase.Timestamp instead of FieldValue.serverTimestamp() in Firebase functions? 有没有办法在没有 Firebase-Admin 的情况下在 Firebase Firestore JS 中使用 FieldValue.serverTimestamp() - Is there any way to use FieldValue.serverTimestamp() in Firebase Firestore JS without Firebase-Admin Swift:如何在 document().setData() Firestore 数据库中使用 FieldValue.serverTimestamp()? - Swift: How to use FieldValue.serverTimestamp() in document().setData() Firestore database? firebase.firestore.FieldValue.arrayUnion 不是 function - firebase.firestore.FieldValue.arrayUnion is not a function Firebase Firestore Increment FieldValue 不递增 - Firebase Firestore Increment FieldValue does not increment 在 Typescript 中使用 FieldValue.serverTimestamp() 的正确方法 - Correct way to use FieldValue.serverTimestamp() in Typescript Cloud Functions - Cloud Firestore 错误:无法获取 serverTimestamp - Cloud Functions - Cloud Firestore error: can't get serverTimestamp Firestore serverTimestamp() 未定义 - Firestore serverTimestamp() undefined 如何将 firebase 中的时间戳数据类型转换为日期? - How to convert timestamp data type in firebase to date? 在 firestore 中“属性‘arrayUnion’在类型‘typeof FieldValue’上不存在” - In firestore "Property 'arrayUnion' does not exist on type 'typeof FieldValue'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM