我有一个显示用户艺术作品的提要。我想按用户发布的类别和时间过滤用户发布的帖子,以便提要的最新帖子更靠近页面顶部。我正在使用 firebase 函数来检索此数据。
我有一个看起来像这样的 firestore 集合
tasks -tasksID-
{
category: art
date: 16 october at 3:00pm
images: {
0 image1
1 image2
}
}
火力基地功能:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
admin.initializeApp()
export const getFeed = functions.https.onCall(async (req,res) =>{
const docs = await admin.firestore().collection('tasks').where('category',
'==', 'art').orderBy('date', 'desc').limit(20).get()
return docs.docs.map(doc => {
return {
postID: doc.id,
...doc.data()
}
})
})
艺术提要 typescript :
artFeed (){
const getFeed = this.aff.httpsCallable('getFeed')
this.ajax = getFeed({}).subscribe(data=> {
console.log(data)
this.posts = data
})
}
但是,我在控制台上收到一条错误消息,显示“错误错误:内部”。
当我单独使用 where() 函数和 orderby() 函数时,此函数工作得非常好。
这也是我的数据库索引的样子。
collectionId Fields indexed Query scope Status
category Ascending
tasks uid Ascending Collection Enabled
date Ascending
tasks category Ascending
uid Ascending Collection Enabled
date Descending
最佳答案
您需要添加特定索引,如下所示:
collectionId Fields indexed Query scope Status
tasks category Ascending Collection Enabled
date Descending
关于angular - 过滤 Firebase 数据时,Where() 和 orderBy() 过滤器不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58419902/