javascript - Firebase 提供失败的前提条件错误
问题描述
使用 ff 代码收到 ff 错误:错误:
{"code":"failed-precondition","name":"FirebaseError","__zone_symbol__currentTask":{"type":"macroTask","state":"notScheduled","source":"setTimeout","zone":"angular","cancelFn":null,"runCount":0}}
我的代码:
getOldPosts(forum_id: string, user_id: string, start_key?: string): Promise<Posts[]> {
return new Promise((resolve, reject) => {
let olderPosts: Posts[] = [];
let _5daysAgo = this.dateTime.getFiveDaysAgoDate();
const postsRef = this.afirestore.collection<Posts>('posts').ref;
const query = start_key == undefined || start_key == null
? postsRef.where('createdDate', '<=', _5daysAgo).where('forumId', '==', forum_id).where('memberId', '==', user_id)
: postsRef.where('createdDate', '<=', _5daysAgo).where('forumId', '==', forum_id).where('memberId', '==', user_id).startAt(start_key).limit(50);
query.get().then(
res => {
res.docs.forEach(doc => {
olderPosts.push(doc.data() as ForumPosts);
});
resolve(olderPosts.reverse());
},
err => {
reject(err);
}
);
});
}
我似乎无法弄清楚为什么会这样。我试过在谷歌上搜索,但我似乎没有找到解决方案。我究竟做错了什么 ?
谢谢。
解决方案
根据管理索引页面中的 Firestore 文档
如果您尝试使用未映射到现有索引的范围子句进行复合查询,则会收到错误消息。
您可以使用 Firebase Firestore Web 控制台为您的查询手动创建索引。
否则,只需运行查询,它就会向 Firestore 控制台抛出带有 URL 的错误,您将在该控制台中获得自动创建此复合索引的选项。
postsRef.where('createdDate', '<=', _5daysAgo).where('forumId', '==', forum_id).where('memberId', '==', user_id)
代码块看起来像罪魁祸首,因为它有一个相等子句和一个范围子句组合。
推荐阅读
- sockets - Dart客户端Socket连接但不向服务器发送数据?
- javascript - 如何在javascript的循环中更改对象的项目值?
- javascript - 反应.js | 如果我将 setState 作为回调函数传递,则无限渲染,即使在解构道具之后也是如此
- javascript - 过滤不同类别并返回多个条目
- glibc - RHEL 6 - 安装了 'GLIBC_2.14' 但仍然是 Yum 错误
- r - 在R中按月和年选择数据
- php - 尝试获取客户订单时得到空结果 Shopify admin api
- java - 如何从命令行启动 JavaFX 源代码文件?
- c# - C# 它在控制台中为其他条件显示 null,而不是我希望它显示的内容
- libgdx - libgdx:如何通过另一个正交相机获得正交相机的结果?