【问题标题】:Filtering Firestore Observable against javascript object values根据 javascript 对象值过滤 Firestore Observable
【发布时间】:2018-02-12 17:12:34
【问题描述】:

我有一个使用 angularfire2 snapshotChanges 函数从 Firestore 查询中获得的 Observable 数据集。我能够获取信息并正常显示,所以一切正常。我遇到困难的是以下几点。 observable 包含用户已经选择包含在其帐户中的团队。当他们通过“添加团队”步进器时,我想突出显示他们已经添加的团队,并且不允许他们重新添加它们。我有一个为用户团队管理数据存储的服务,我想为该服务添加一个独特的检查。

鉴于这个背景,这里是服务功能,uniqCheck,我已经搞砸了三天,无法开始工作。 .do 方法不输出任何内容,这让我相信 .map 方法没有做我认为应该做的事情,但是控制台中没有抛出任何错误。我不必返回数据库来验证这一点,因为我已经在客户端中拥有了 observable。我已经包含了我的加载功能以供参考。 uniqCheck 函数正是我要解决的问题。

我目前正在尝试什么,I found here

有什么建议吗?

private _collection: AngularFirestoreCollection<any>;
public store_userteams: Observable<any[]> = null;

load(): Promise<any> {
  return new Promise(resolve => {
    // console.log('load user team records for user ' + this._session.currentuser.uid);
    this._collection = this._firedb.collection<any>('userteams', ref => ref.where('userid', '==', this._session.currentuser.uid ));
    this.store_userteams = this._collection.snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    });
    resolve(true);
  });
}

// tta is a javascript collection passed in from the component function.  tta.id is valid and correct
uniqCheck(tta): Promise<any> {
 return new Promise(resolve => {
   let matchcount = 0;
   console.log(tta.id);
   this.store_userteams
   .map( teams => teams.filter(team => team.teaminfo.id===tta.id) )
   .do( teams => console.log('filtered teams: ' + teams) )
   .subscribe( teams => matchcount = teams.length );
   console.log('matchcount: ' + matchcount);
   resolve(matchcount);
 });
}

【问题讨论】:

  • 为什么 uniqCheck 函数返回一个 Promise,它可以是一个简单的返回值函数,在 store_userteams 上使用 undersocrejs 和/或过滤器,我认为这应该为你工作。
  • 那是我真正开始的,但由于 store_userteams 是一个 Observable,下划线不能使用它。理想情况下,只有一种方法可以从 store_userteams 中获取基础数据,但似乎并非如此。需要澄清的是,store_userteams 是 Firestore 查询的结果。

标签: angular firebase google-cloud-firestore


【解决方案1】:

试试这个,下面的解决方案不会使 store_userteams 成为 Observable 并且 id 将出现在每个对象中,因此很容易执行与 JavaScript 相关的过滤器(下划线或原版)

load() {
    this.loadData().subscribe(data => {
        this.store_userteams = _.pluck(data, 'data');
    });
}
loadData() {
    this._collection = this._firedb.collection<any>('userteams', ref => ref.where('userid', '==', this._session.currentuser.uid ));
    return this._collection.snapshotChanges().map(actions => {
        return actions.map(a => {
            const id = a.payload.doc.id;
            const data = a.payload.doc.data();
            data.id = id;

            return { data };
        });
    });
}

现在在uniqCheck 函数中,您可以使用store_userteams 对象

uniqCheck(tta) {
    // underscore or vanilla js filter on this.store_userteams
    console.log(tta);
    console.log('this.store_userteams : ', this.store_userteams);
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2023-04-03
    • 2015-05-30
    • 2020-12-03
    相关资源
    最近更新 更多