【问题标题】:Filter Ionic cards by stored firebase preferences通过存储的 Firebase 首选项过滤 Ionic 卡
【发布时间】:2019-01-21 17:57:41
【问题描述】:

我有一个 Firestore 数据库,其中包含 2 个不同的对象集合,即用户和属性。我希望能够根据用户存储的偏好向用户显示属性提要中的相关属性卡(即,如果他们的城市偏好保存为“都柏林”,则仅显示位于都柏林的属性)。

我的属性提要组件目前如下所示:`

export class PropertyFeedComponent implements OnInit {

  Propertys;
  filtered;

  Property: Observable<any[]>;


  filter = new BehaviorSubject(null);

  constructor(
    public db: DbService,
    public modal: ModalController,
    public auth: AuthService
  ) {}

  ngOnInit() {
    this.Propertys = this.auth.user$.pipe(
      switchMap(user =>
        this.db.collection$('Propertys', ref =>
          ref
            .orderBy('createdAt', 'desc')
            .limit(100)
        )
      ),
      shareReplay(1)
    );

   // this.filtered = this.filter.pipe(
    //  switchMap(status => {
    //    return this.Propertys.pipe(

    //    );
    //  })
   // );
  }
}

` 我有一个过滤函数被注释掉,因为我不确定这是否是最好的方法。 我在我的 HTML 中从数据库中调用属性,如下所示:

   <ng-container *ngIf="(auth.user | async) || {} as user">


  <ion-card *ngFor="let Property of Propertys | async; trackBy: trackById">

如何查询每个单独的对象以查看它是否与用户存储的偏好匹配。

【问题讨论】:

    标签: angular firebase ionic-framework ionic4


    【解决方案1】:
    this.Propertys = this.auth.user$.pipe(
      switchMap(user =>
        this.db.collection$('Propertys', ref =>
          ref
            .where('location','==',user.prefferedlocation)
            .orderBy('createdAt', 'desc')
            .limit(100)
        )
      ),
      shareReplay(1)
    );
    

    locationProperty 集合中的字段。 User.prefferedlocation 是您在 Firestore 数据库中存储用户偏好的位置。

    您可以在每次调用函数时获取首选项,也可以将首选项保存在浏览器的本地变量中,以避免对数据库进行不必要的读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多