【问题标题】:how to get Id of documents in Firestore using useCollectionData hook如何使用 useCollectionData 钩子在 Firestore 中获取文档的 ID
【发布时间】:2022-10-06 03:05:58
【问题描述】:
const messagesRef = collection(db, \'messages\')
const q = query(messagesRef, orderBy(\'createdAt\'), limit(25))
const [messages, loading] = useCollectionData(q)
// messages print fields of documents without id

需要获取文档的 ID,我想有一种方法可以在该钩子中传递选项,但 dk 如何。

如果没有办法使用该钩子获取 Id,那么选项是什么?

  • 请在此处发布之前尝试解决您的问题。你读过文档吗?你试过什么了?为什么你的尝试没有成功,等等。也请阅读tour,阅读How to Ask 以及如何制作minimal reproducible example

标签: reactjs firebase google-cloud-firestore react-hooks


【解决方案1】:

好吧,我把钩子改成了 useCollection

const [messages, loading] = useCollection(q)

这解决了一个问题。 useCollectionData 只返回文档的字段,而 useCollection 返回每​​个文档和 id 的 snapshot

【讨论】:

    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 2022-07-08
    相关资源
    最近更新 更多