【问题标题】:React-native-firebase server timestamp on offline queries离线查询的 React-native-firebase 服务器时间戳
【发布时间】:2020-11-06 11:59:42
【问题描述】:

我正在尝试使用服务器时间戳字段上的 where 条件进行查询,工作很完美,但是当它离线时,快照不会触发,直到它再次在线。当我删除 where 条件时工作正常。

是否可以使用此服务器时间戳字段脱机工作?还是我做错了什么?

这是我的查询:

firestore()
  .collectionGroup(collectionNames.MESSAGES)
  .where('chatId', 'in', chatIds)
  .where('updatedAt', '>', lastMessageDate)
  .onSnapshot({ includeMetadataChanges: true }, onMessage, onError)

这是我的对象:

create({ chatId, type, text, url, user }) {
  const { id, email, name, avatarUrl } = user
    this.chatId = chatId
    this.type = type
    this.text = text
    this.url = url
    this.ownerId = id
    this.ownerEmail = email
    this.ownerName = name
    this.ownerAvatarUrl = avatarUrl
    this.updatedAt = firestore.FieldValue.serverTimestamp()
    return this.toJSON()
}

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore react-native-firebase


    【解决方案1】:

    由于服务器时间戳是在服务器上评估的,因此在文档最终同步之前它们没有实际值。这意味着您将无法使用该字段进行过滤 - 在服务器上发生写入之前,该值实际上为 null。

    您在指定 SnapshotOptions 时观察到的时间戳估计行为仅在您手头有实际 DocumentSnapshot 时才有效。您无法过滤时间戳的估计值。

    【讨论】:

      【解决方案2】:

      firestore.FieldValue.serverTimestamp() 被评估为服务器端,因此您将无法离线使用它。这里有一篇好文章https://medium.com/firebase-developers/the-secrets-of-firestore-fieldvalue-servertimestamp-revealed-29dd7a38a82b

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-30
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 2020-05-03
        • 2020-10-24
        • 2019-03-09
        相关资源
        最近更新 更多