在Firestore中跨不同集合查询数据

时间:2018-11-23 18:40:14

标签: firebase google-cloud-firestore

我有2个收藏集:一个收藏报纸,另一个收藏给用户注册: enter image description here

enter image description here

此刻我正在展示我的报纸: enter image description here

书签按钮(左侧的按钮)将报纸添加到用户集合中(如第二幅图所示),但是在获取报纸时我没有进行检查。我该如何更改按钮颜色?

这是我获取报纸以及将其保存到用户收藏夹中的方式:

getDefaultNewspapers() {
    this.newspapers$ = this.likes$.pipe(
      switchMap(likes =>
        this.angularFirestore
          .collection<NewspaperModel>("newspapers", ref =>
            ref.where("likes", ">=", likes).orderBy("likes", "desc") // forget about this filter
          )
          // .valueChanges()
          .snapshotChanges() // necessary for metadata like IDs
          .map(newspapers => {
            return newspapers.map(newspaper => {
              const $id = newspaper.payload.doc.id;
              const data = newspaper.payload.doc.data() as NewspaperModel;
              return { $id, ...data };
            });
          })
      )
    );
  }

  addRemoveFavourite(newspaperId: string) {
    this.angularFirestore
      .collection("users")
      .doc(this.afAuth.auth.currentUser.uid)
      .update({
        ["myNews." + newspaperId]: {
          favourite: false,
          visits: 0
        }
      });
  }

0 个答案:

没有答案