【问题标题】:Angualar firestore _methodName FieldValuea.serverTimestampAngular firestore methodName FieldValue.serverTimestamp
【发布时间】:2020-03-31 10:03:51
【问题描述】:

我需要将服务器时间戳存储到我的 Firestore 文档中
从互联网上我得到了这个代码firebase.firestore.FieldValue.serverTimestamp(),但是在我在 Firestore 中使用这个代码之后,它存储为这样_methodName FieldValuea.serverTimestamp
这是什么问题以及如何解决? 我的 ts 代码 `

 let m1 = new Message();
    m1.doctor = this.uid;
    m1.patient = this.messageList[this.messageList.length - 1].patient;
    m1.isPatient = false;
    m1.message = event.message;


    m1.date = firebase.firestore.FieldValue.serverTimestamp(),
      m1.reply = true,
      // type: files.length ? 'file' : 'text',
      //files: files,
      m1.user = {
        name: 'Jonh Doe',
        avatar: 'https://i.gifer.com/no.gif',
      },

      this.firestore.collection(Config.chatCollection).add(JSON.parse(JSON.stringify(m1)));
`



class Message {
  user = {
    name: 'Jonh Doe',
    avatar: 'https://i.gifer.com/no.gif',
  }
  reply;
  doctor = "";
  text;
  date;
  cdate;
  isPatient = true;
  message = "";
  patient = "";
  time = "";
  documentID = "";
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore angularfire2


    【解决方案1】:

    这很可能是因为你正在这样做

    this.firestore.collection(Config.chatCollection).add(JSON.parse(JSON.stringify(m1)));
    

    通过做

    this.firestore.collection(Config.chatCollection).add(m1);
    

    应该没问题,因为m1 似乎是一个 JavaScript 对象。

    add() 方法的文档是here,你会看到它的参数应该是一个“包含新文档数据的对象”。如果m1是一个合法的Object,则不需要做JSON.parse(JSON.stringify(m1)),对应:

    1. m1 JavaScript 对象转换为 JSON 字符串,以及 那么;
    2. 获取生成的 JSON 字符串并将其转换回 JavaScript 对象。

    【讨论】:

    • ERROR FirebaseError: Function CollectionReference.add() 要求它的第一个参数是对象类型,但它是:一个对象
    • 已更新
    • 你能检查m1是否是一个对象。例如。通过使用stackoverflow.com/questions/8511281/…
    • PS:您确定您的代码不包含我们在您的问题中可以看到的错字:_methodName FieldValuea.serverTimestampFieldValuea 带有一个额外的 a 而不是 FieldValue
    猜你喜欢
    • 2021-03-19
    • 1970-01-01
    • 2022-08-17
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2019-12-16
    相关资源
    最近更新 更多