过滤 Firebase 数据时,Where() 和 orderBy() 过滤器不能一起工作

6 firebase typescript google-cloud-platform google-cloud-functions angularfire2

我有一个向用户展示艺术作品的提要。我想按用户发布帖子的类别和时间过滤用户帖子,以便该提要的最新帖子更接近页面顶部。我正在使用 firebase 函数来检索此数据。

我有一个 firestore 集合,看起来像这样

 tasks -tasksID- 
                {
                   category: art
                   date: 16 october at 3:00pm
                     images: {
                                0 image1
                                1 image2
                             }
                  }
Run Code Online (Sandbox Code Playgroud)

火力基地功能:

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()
         }
     }) 
 })
Run Code Online (Sandbox Code Playgroud)

艺术提要打字稿:

artFeed (){    
  const getFeed = this.aff.httpsCallable('getFeed')
  this.ajax = getFeed({}).subscribe(data=> {
    console.log(data)
    this.posts =  data
      })  
  }
Run Code Online (Sandbox Code Playgroud)

但是,我在控制台上收到错误消息“ERROR Error: INTERNAL”。

当我单独使用 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
Run Code Online (Sandbox Code Playgroud)

Ren*_*nec 6

您需要添加特定索引,如下所示:

collectionId      Fields indexed       Query scope            Status 

              
tasks             category Ascending   Collection             Enabled 
                  date Descending
Run Code Online (Sandbox Code Playgroud)