Firebase 函数:orderBy 函数不起作用
Firebase Functions: orderBy function not working
以下代码在模拟器中有效,但 returns 当 includeVideos
或 includeAudios
为 false
时,firebase 中的空数组:
let query = db.collection("content");
if (includeVideos && !includeAudios) {
query = query.where("type", '==', "video").orderBy("type");
}
if (includeAudios && !includeVideos) {
query = query.where("type", '==', "audio").orderBy("type");
}
if (!includeVideos && !includeAudios) {
return {empty json....}
}
if (matchingPersonID != undefined) {
query = query.where("attributionID", '==', matchingPersonID).orderBy("attributionID");
}
//either categories OR matchingSeriesID is always undefined/empty
if (matchingSeriesID != undefined) {
query = query.where("seriesIDs", 'array-contains', matchingSeriesID).orderBy("seriesIDs");
}
if (matchingCategories.length != 0) {
// 'OR' LOGIC
query = query.where("categories", 'array-contains-any', matchingCategories).orderBy("categories");
}
if (lastDocumentID != undefined) {
await db.collection("content").doc(lastDocumentID).get().then((snapshot) => {
query = query.startAfter(snapshot);
log(`starting after document ${snapshot}`)
})
}
//the code works when this line is removed, but the query needs to be sorted by time in order for pagination to work
query = query.orderBy("upload_date", "desc");
return query.limit(getNext).get()...
我在 google 云日志中没有收到任何错误消息,因此我没有创建索引的简单方法(如果我需要这样做的话)。
在firestore中,字段upload_date
、attributionID
、type
、seriesIDs
、categories
在文档中都是同级的
任何帮助将不胜感激!!
根据您的评论,该字段的 index 似乎缺失。
This documentation提到了两种处理这些索引的方法:
通过使用 CLI。您可以将索引以 JSON 格式放入本地文件。为此,运行 firebase firestore:indexes
在你的 firebase 项目文件夹中,你会得到一个格式化的 JSON 输出供你复制,添加一个新的然后用 firebase deploy --only firestore:indexes
部署它们.
确实,您可以使用 Firebase 在捕获到使用不存在的索引的错误时生成的 URL,但您也可以从 Firebase 控制台手动执行应用程序:
以下代码在模拟器中有效,但 returns 当 includeVideos
或 includeAudios
为 false
时,firebase 中的空数组:
let query = db.collection("content");
if (includeVideos && !includeAudios) {
query = query.where("type", '==', "video").orderBy("type");
}
if (includeAudios && !includeVideos) {
query = query.where("type", '==', "audio").orderBy("type");
}
if (!includeVideos && !includeAudios) {
return {empty json....}
}
if (matchingPersonID != undefined) {
query = query.where("attributionID", '==', matchingPersonID).orderBy("attributionID");
}
//either categories OR matchingSeriesID is always undefined/empty
if (matchingSeriesID != undefined) {
query = query.where("seriesIDs", 'array-contains', matchingSeriesID).orderBy("seriesIDs");
}
if (matchingCategories.length != 0) {
// 'OR' LOGIC
query = query.where("categories", 'array-contains-any', matchingCategories).orderBy("categories");
}
if (lastDocumentID != undefined) {
await db.collection("content").doc(lastDocumentID).get().then((snapshot) => {
query = query.startAfter(snapshot);
log(`starting after document ${snapshot}`)
})
}
//the code works when this line is removed, but the query needs to be sorted by time in order for pagination to work
query = query.orderBy("upload_date", "desc");
return query.limit(getNext).get()...
我在 google 云日志中没有收到任何错误消息,因此我没有创建索引的简单方法(如果我需要这样做的话)。
在firestore中,字段upload_date
、attributionID
、type
、seriesIDs
、categories
在文档中都是同级的
任何帮助将不胜感激!!
根据您的评论,该字段的 index 似乎缺失。
This documentation提到了两种处理这些索引的方法:
通过使用 CLI。您可以将索引以 JSON 格式放入本地文件。为此,运行
firebase firestore:indexes
在你的 firebase 项目文件夹中,你会得到一个格式化的 JSON 输出供你复制,添加一个新的然后用firebase deploy --only firestore:indexes
部署它们.确实,您可以使用 Firebase 在捕获到使用不存在的索引的错误时生成的 URL,但您也可以从 Firebase 控制台手动执行应用程序: