【问题标题】:How to order database list asc or desc如何订购数据库列表 asc 或 desc
【发布时间】:2018-09-09 16:06:49
【问题描述】:

我正在尝试在 ionic3 和 Angularfire2 中查询有序列表。

通过这个查询,它显示了从最低到最高评分的餐厅,但我如何从高到低查询?

查询:

this.restaurantsRef = afDb.list('/restaurants', ref => ref.orderByChild('rating'));

我怎样才能进行多重查询?因为用户想要在多个条件下过滤餐厅。

AngularFire 版本:

"angularfire2": "^5.0.0-rc.6",

文档是这样说的:

// Order descending by numbers or strings
afs.collection('people', ref => ref.orderBy('age', 'desc') )
afs.collection('people', ref => ref.orderBy('name', 'desc') ) // reverse alphabetical

但是当我尝试orderBy 时,它返回了这个错误:

ref.orderBy is not a function

OrderBy 似乎在类型 Reference 上不存在

有人可以帮我吗?

【问题讨论】:

  • 请记住 orderBy 是针对查询而不是针对数据库引用,因此 ref 需要来自查询
  • @GastónSaillén 当我从那个副本中尝试它时,它说它不能分配给 QueryFn 类型的参数。 QueryFn 类型中不存在查询
  • @GastónSaillén 你能告诉我你的意思是 ref 需要来自查询
  • Orderby 来自 Query query = your ref ,而不是来自 ref 本身

标签: firebase ionic3 angularfire2


【解决方案1】:

好的,我找到了解决方案,我写了一个这样的管道:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'reverse'
})
export class ReversePipe implements PipeTransform {

  transform(value) {
      if (!value) return;

      return value.reverse();
    }
}

然后像这样在html中使用它:

 *ngFor="let restaurant of restaurantsList | async | reverse" 

【讨论】:

    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2020-08-31
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多