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

标签 angular typescript firebase google-cloud-functions angularfire2

我有一个显示用户艺术作品的提要。我想按用户发布的类别和时间过滤用户发布的帖子,以便提要的最新帖子更靠近页面顶部。我正在使用 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/

相关文章:

asp.net-web-api - 无法在 ASP.NET Core 1.0 中启用跨源请求 (CORS)

typescript 装饰二传手功能

javascript - React-file-viewer 将任何文件渲染得非常小

android - 像地理围栏一样使用 geofire

rest - Firebase 身份验证 + 自己的 API

ios - 如何订购 Firebase 数据?

javascript - 如何在 Angular API 请求的 header 中传递 x-api-key?

angular - 如何在 Angular 4 中实现 OpenID

html - Angular Material 和输入类型 'time' ,值属性与 ngModel

angular - karma 不断要求更多与测试组件无关的进口