简体   繁体   English

NextJS: react-firebase-hooks orderBy

[英]NextJS: react-firebase-hooks orderBy

I am using this package https://github.com/csfrequency/react-firebase-hooks and the code below works.我正在使用这个 package https://github.com/csfrequency/react-firebase-hooks并且下面的代码有效。

const [posts, loading, error] = useCollection(
    firebase
      .firestore()
      .collection('posts')
      .where('uid', '==', uid),

    {
      snapshotListenOptions: { includeMetadataChanges: true },
    }
  )

However, when I add the orderBy to it, I got an error: Error: {"code":"failed-precondition","name":"FirebaseError"}但是,当我向其中添加 orderBy 时,出现错误: Error: {"code":"failed-precondition","name":"FirebaseError"}

  const [posts, loading, error] = useCollection(
    firebase
      .firestore()
      .collection('posts')
      .where('uid', '==', uid)
      .orderBy('createdAt', 'desc'),
    {
      snapshotListenOptions: { includeMetadataChanges: true },
    }
  )

I wonder how should the query be constructed when using React-Firebase-Hooks.我想知道在使用 React-Firebase-Hooks 时应该如何构造查询。 Any help would be greatly appreciated.任何帮助将不胜感激。

I think you need to add composite index:我认为您需要添加复合索引:

collection post: uid Ascending createdAt Descending

Not sure how you get this error, but maybe you are able to get more details from it.不知道你是如何得到这个错误的,但也许你可以从中得到更多的细节。 I replicated it directly in node.我直接在节点中复制了它。 And if you run such query directly in node you will get error massage with more details like:如果您直接在节点中运行此类查询,您将获得错误消息,其中包含更多详细信息,例如:

Error: 9 FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/<project-id>/firestore/indexes?create_composite=Ck1wcm9qZWN0cy9zdXJ5amFrLXRlc3QtMDAvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL3Bvc3RzL2luZGV4ZXMvXxABGgcKA3VpZBABGggKBHNvcnQQAhoMCghfX25hbWVfXxAC

If you hit the link in the error the index will be build automatically.如果您点击错误中的链接,索引将自动构建。 The documentation to composite indexes is here复合索引的文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM