尝试根据当前用户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 中添加索引。没有运气。
Firestore 查询基于与每个查询的条件和顺序完全匹配的索引 的存在进行工作。默认情况下,Firestore 会自动为文档的所有单独字段编制索引,这适用于大多数简单查询。
如果你在不同的字段上同时有一个 where 子句和一个 order by,你将需要在这两个字段上建立一个复合索引,并且这样的索引不会自动创建。您将不得不手动创建索引,这很可能是您忘记做的。
创建缺失索引有两种方法: