Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Firebase 条件分岐をしてデータの読み取り(Firestore)

More than 1 year has passed since last update.

はじめに

FirebaseのデータベースFirestoreで、条件分岐をしてからデータを取得する例です。
また、取得したデータの数をカウントしてます。

私の環境
・Ionic + Angular

whereを使う

eventの中身がこんな感じだとする。

event.model.ts
export class Event {
         constructor(
           public id?: string,
           public title?: string,
           public periods?: boolean,
         ) {}
       }
home.page.ts
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss']
})
export class HomePage implements OnInit {
private eventCount;
 private periodCount;


constructor(
    private afAuth: AngularFireAuth,
    private db: AngularFirestore
  ) { }

  ngOnInit() {
    this.afAuth.auth.onAuthStateChanged((user) => {
      if (user != null) {

      // events以下の全てのドキュメントを取得してカウント
        this.db
          .collection(`users/${user.uid}/events`)
          .snapshotChanges()
          .subscribe(c => {
            this.eventCount = c.length;
          });

      // events以下のドキュメントでperiodsプロパティがtrueのドキュメントを取得
        this.db
           .collection(`users/${user.uid}/events`, ref =>
             ref.where('periods', '==', true)
           )
           .snapshotChanges()
           .subscribe(c => {
             this.periodCount = c.length;
           });
      }
    });
  }

参考

}

kokogento
エストニアの電子国民e-Residencyです。2020/09より、東京で人生初の正社員に! 〇活を記録するアプリをIonic + Angularで開発し、運営中。アプリについては下記URLを参照。現在Vueを学習中。。。
https://gengendiary.world/first-app/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away