为什么我不能在带角的firestore数据库中添加任何字段?

时间:2019-02-26 17:21:45

标签: angular firebase google-cloud-firestore

我的组件看起来像这样:

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

import { AngularFirestore } from 'angularfire2/firestore';

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

  public items: Observable<any[]>;

    constructor(db: AngularFirestore) {
        this.items = db.collection('/todos').valueChanges();
    }
    logForm(value: any) {
      console.log(value);

      this.db.collection("/todos").add({
          name: "Los Angeles",
          state: "CA",
          country: "USA"
      });

    }
}

但是我不明白为什么this.db有问题,并且每次遇到此错误时,Property 'db' does not exist on type 'MissionComponent'

可能我在这里丢失了一些东西。

1 个答案:

答案 0 :(得分:1)

db仅在构造函数范围内有效。

export class MissionComponent {

  public items: Observable<any[]>;
  private db: AngularFirestore; // this will be this.db

    constructor(db: AngularFirestore) {
        this.items = db.collection('/todos').valueChanges();
        this.db = db; // now you can use this.db
    }