检索firestore集合,使用angular4文档

时间:2019-03-21 14:33:26

标签: angular firebase google-cloud-firestore

我有firebase数据库,并尝试通过angular检索数据库。 能够连接到数据库,但是,无法读取数据...这是我所做的并且无法获取数据...

我肯定会丢失一些东西,任何人都可以指导或提供链接吗?

import { Component } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import {AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from 'angularfire2/firestore';
import { Http } from '@angular/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private itemsCollection: AngularFirestoreCollection<UserProfiles>;
  items: Observable<UserProfiles[]>;
  constructor(private afs: AngularFirestore) {
    this.itemsCollection = afs.collection<UserProfiles>('UserProfiles');
    this.items = this.itemsCollection.valueChanges();
  }

}

export class UserProfiles{
  id?: string;
  active?: string;
  company?: string;
  email?: string;
  firstName?: string;
  lastName?: string;
  password?: string;
  userId?: string;
  userType?: string;
}

1 个答案:

答案 0 :(得分:0)

您需要针对subscribe()创建的Observable<UserProfiles[]>;(在这种情况下为valueChanges()()类属性)items进行检索/使用数据:

import { Component } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import {AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from 'angularfire2/firestore';
import { Http } from '@angular/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private itemsCollection: AngularFirestoreCollection<UserProfiles>;
  items: Observable<UserProfiles[]>;
  constructor(private afs: AngularFirestore) {
    this.itemsCollection = afs.collection<UserProfiles>('UserProfiles');
    this.items = this.itemsCollection.valueChanges();
    this.items.subscribe(data => console.log(data)); // subscribe and log emitted data
  }

}

否则,如果您在模板中使用items,作为一种选择,您可以利用async管道来订阅items的可观察对象并呈现数据:

<ul>
  <li *ngFor="let item of items | async">{{item.someProperty}}</li>
</ul>

希望有帮助!