1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AngularでFirebase(Firestore)からドキュメントの数を取得

Posted at

Firestore内のドキュメントの総数を取得

もしログインしているユーザーが作成したイベントの数を知りたければ

import { Component, OnInit } from '@angular/core';
import { User } from '../auth/auth.service';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';

@Component({
  selector: 'app-my-page',
  templateUrl: './my-page.page.html',
  styleUrls: ['./my-page.page.scss'],
})
export class MyPagePage implements OnInit {
  private currentUser: User;
  private myCount: number;

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

  ngOnInit() {
    this.afAuth.auth.onAuthStateChanged((user) => {
      if (user != null) {
        this.currentUser = user;
        this.db.collection(`users/${user.uid}/events`)
          .snapshotChanges().subscribe(c => {
            this.myCount = c.length;

         // 今ログインしてるユーザーが作成したイベントの総数をコンソールに表示
            console.log(this.myCount);
            return c.length;
          });
      }
    });
  }
}

Firestore内はこんな感じ。

スクリーンショット 2019-10-07 19.08.11.jpg

ちなみに、「ドキュメント」とは

ドキュメントとはここでは書類の事ではなくて、Firestore内でのデータ1つ分の事を言う。

そしてドキュメントの集まりを**「コレクション」**と言う。

上の画像の例で言えば、usersとeventsがコレクション。

そのコレクション内にあるユニークid1つ1つのことを**「ドキュメント」**と呼んでいる。

まぁ知っといて損はないかと。。。。

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?