如何使用Firebase Firestore进行角度查询?

时间:2018-11-24 00:28:10

标签: angular typescript firebase google-cloud-firestore angularfire2

我正在尝试使用Firebase Firestore在angular 6中进行查询,我有此代码,我已经下载了packege“ npm firebase @angularfire”,但不起作用:

    import { Component } from '@angular/core';
    import { AngularFirestore, AngularFirestoreCollection } from 
    '@angular/fire/firestore';
    import {Lesson} from './models/lesson.model'
    import { Observable} from 'rxjs';
    import {BehaviorSubject} from 'rxjs/BehaviorSubject';
    import {switchMap} from 'rxjs/operators'; 
    import * as moment from 'moment';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {

      lessonRef:AngularFirestoreCollection<Lesson>;
      lesson$: Observable<Lesson[]>;
      endDate$: BehaviorSubject<Date>;

      constructor(afs:AngularFirestore){
        this.endDate$ = new BehaviorSubject(new Date('2017-12-24'));

        this.lesson$= this.endDate$.pipe(
          switchMap(date=>
          afs.collection<Lesson>('Lesson', ref => 
          ref.where('endDate', "==", date))
          .valueChanges(),
          ),
        );

      }

1 个答案:

答案 0 :(得分:0)

您没有在任何地方定义ref。正如AngularFire documentation所说:

  

通过在firebase.firestore.CollectionReference上构建查询来创建查询。

您需要定义一个CollectionReference,如Firestore documentation所示:

let ref = firebase.firestore().collection("Lessons");

然后,您可以定义一个AngularFire2集合,并在该集合的基础上使用已有的代码进行查询。