.where 不显示使用 react-firebase-hooks 的结果

问题描述 投票:0回答:1

尝试根据当前用户id获取查询结果。它存储在“uid”字段下的文档集合中。

这是我的代码:

function Channels() {
const currentUid = auth.currentUser.uid;
const query = firestore.collection('multitwitch').where('uid', '==', currentUid).orderBy('createdAt');

const [channels] = useCollectionData(query);
  
  return (<>
    <main>
      <div>
      {channels && channels.map(chan => <ChannelStrip key={chan.id} channel={chan} />)}                                                                                                                          
      </div>
    </main>
  </>)
}


function ChannelStrip(props) {
  const {twitchchannel} = props.channel;

  return (<>
<iframe
    src={ `https://player.twitch.tv/?channel=${twitchchannel}&parent=localhost&muted=true` }
    height="300"
    width="500"
    allowfullscreen>
</iframe>
  </>)
}

当我在查询中添加 .where 时,这显示没有结果。

但是当我删除 .orderBy 时给出结果。

这是为什么?

还尝试在 firestore 中添加索引。没有运气。

javascript reactjs firebase google-cloud-firestore where-clause
1个回答
0
投票

Firestore 查询基于与每个查询的条件和顺序完全匹配的索引 的存在进行工作。默认情况下,Firestore 会自动为文档的所有单独字段编制索引,这适用于大多数简单查询。

如果你在不同的字段上同时有一个 where 子句和一个 order by,你将需要在这两个字段上建立一个复合索引,并且这样的索引不会自动创建。您将不得不手动创建索引,这很可能是您忘记做的。

创建缺失索引有两种方法:

  • 如果您捕获并记录代码中的错误,输出将包含有关缺少索引的错误消息。此错误消息包含指向 Firestore 控制台的链接,其中包含有关预填充的缺失索引的所有信息。所以:打开该链接,单击按钮,等待索引建立,然后重试查询。这是creating a missing index through an error message
  • 中的文档
  • 您也可以手动创建索引,如在 Firebase 控制台中创建索引
  • 的文档所示
© www.soinside.com 2019 - 2024. All rights reserved.