【问题标题】:How do I create a Firestore query for documents created after a timestamp in the new modular sdk?如何为在新模块化 sdk 中的时间戳之后创建的文档创建 Firestore 查询?
【发布时间】:2021-09-02 23:52:22
【问题描述】:

我对 Firestore 比较陌生,更不用说新的模块化 sdk,我想知道是否有人知道为什么此查询返回的文档的 createdAt 时间戳早于我提供的时间戳?我只想要在我提供的时间戳之后创建的文档。

const timeRef = useRef(Date.now());

const notificationQuery = query(
   collection(clientFirestore, `users/${uid}/notification_history`),
   orderBy("createdAt"),
   where("createdAt", ">=", timeRef.current),
   limit(1)
 );

我也试过这个:

const notificationQuery = query(
   collection(clientFirestore, `users/${uid}/notification_history`),
   orderBy("createdAt"),
   startAfter(timeRef.current),
   limit(1)
);

是我误用了日期还是查询错误?

【问题讨论】:

    标签: reactjs firebase google-cloud-firestore


    【解决方案1】:

    这似乎不合逻辑:useRef(Date.now())Date.now() 给你一个数字,而不是你可以传递给useRef 的路径。

    你在找Timestamp.now()吗?

    where("createdAt", ">=", Timestamp.now()),
    

    【讨论】:

    • 天哪,非常感谢。您不仅在几分钟内回复了,而且确实正是我想要的。我不知道为什么我认为 Date.now() 有一点点意义。我实际上已经查看了 serverTimestamp,并像这样导入它: import { serverTimestamp } from "firebase/firestore";但我很快了解到它只能与 update() 和 set() 一起使用,因为如果我理解正确的话,它是一种占位符值,可以在 DB 端进行解释。我希望我能对你的回答投票 10 次以上,你是绝对的救星!
    猜你喜欢
    • 2020-03-04
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2021-11-25
    • 2020-03-03
    • 2019-12-10
    • 2018-03-28
    • 2021-11-17
    相关资源
    最近更新 更多