【问题标题】:Unsupported field value: a function (found in field creationTime)不支持的字段值:一个函数(在字段创建时间中找到)
【发布时间】:2019-07-22 02:19:12
【问题描述】:

user-profile.ts

export interface UserProfile {
    id: string;
    name: string;
    email: string;
    phone?: string;
    creationTime?: any;
}

service.ts

async signUp(name: string, email: string, password: string): Promise<void> {
    try {
      const user: firebase.auth.UserCredential = await this.afAuth.auth.createUserWithEmailAndPassword(email, password);
      const userProfileDocument: AngularFirestoreDocument<UserProfile> = this.fireStore.doc(`userProfiles/${user.user.uid}`);
      const userProfile: UserProfile = {
        id: user.user.uid,
        email: email,
        creationTime: firebase.firestore.Timestamp,
        name: name
      };
      await userProfileDocument.set(userProfile);
    } catch (error) {
    }
  }

控制台出现一个错误:

[2019-02-28T08:01:58.804Z] @firebase/firestore: Firestore (5.8.3):
timestampsInSnapshots 设置现在默认为 true,而您没有
不再需要显式设置它。在未来的版本中,设置
将被完全删除,因此建议您将其删除
现在从您的 firestore.settings() 调用中。

这是下一个错误:

FirebaseError:调用无效的函数 DocumentReference.set() 数据。不支持的字段值:一个函数(在字段中找到 创建时间) 在新的 FirestoreError (http://localhost:8100/vendor.js:77381:28) 在 ParseContext.push../node_modules/@firebase/firestore/dist/index.cjs.js.ParseContext.createError (http://localhost:8100/vendor.js:96125:16) 在 UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseScalarValue (http://localhost:8100/vendor.js:96467:27) 在 UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseData (http://localhost:8100/vendor.js:96338:29) 在http://localhost:8100/vendor.js:96354:41 在 forEach (http://localhost:8100/vendor.js:77483:13) 在 UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseObject (http://localhost:8100/vendor.js:96353:13) 在 UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseData (http://localhost:8100/vendor.js:96312:25) 在 UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseSetData (http://localhost:8100/vendor.js:96177:31) 在 DocumentReference.push../node_modules/@firebase/firestore/dist/index.cjs.js.DocumentReference.set (http://localhost:8100/vendor.js:97049:45)

【问题讨论】:

标签: angular typescript firebase google-cloud-firestore ionic4


【解决方案1】:

你需要这样做:

第一个错误: https://github.com/angular/angularfire2/issues/1993#issuecomment-456481677

第二次错误:

  const userProfile: UserProfile = {
    id: user.user.uid,
    email: email,
    creationTime: firebase.firestore.FieldValue.serverTimestamp(),
    name: name
  };

如文档中所述,firebase.firestore.FieldValue.serverTimestamp() 将返回一个“哨兵”对象,您在构建要写入 Firestore 的对象时使用该对象。它将向服务器指示它应该用实际的时间戳替换它。

【讨论】:

  • 现在没有运行时错误和以上的作品。但是你知道为什么会出现这个控制台错误吗? [2019-02-28T08:39:02.572Z] @firebase/firestore: Firestore (5.8.3): The timestampsInSnapshots setting now defaults to true and you no longer need to explicitly set it. In a future release, the setting will be removed entirely and so it is recommended that you remove it from your firestore.settings() call now.
  • 在 5.8.3 之前,您必须手动将 timestampsInSnapshots 设置为 trueconst settings = {/* your settings... */ timestampsInSnapshots: true}; admin.firestore().settings(settings);。您提到的信息消息只是表明您不再需要这样做。
  • 我没有设置任何东西。那么我应该在哪里查看这个设置呢?
  • 这对我有用:github.com/angular/angularfire2/issues/… 谢谢。
  • 消息说您不再需要在您的firestore.settings() 呼叫中添加timestampsInSnapshots: true。所以如果你没有打电话给firestore.settings(),你不需要做任何事情。
猜你喜欢
  • 2020-06-29
  • 2012-11-15
  • 2019-04-25
  • 2020-04-24
  • 2021-12-13
  • 2019-12-20
  • 2018-08-11
  • 2013-06-13
  • 2021-06-12
相关资源
最近更新 更多