Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 为什么我能';是否在firestore数据库中添加任何字段?_Angular_Firebase_Google Cloud Firestore - Fatal编程技术网

Angular 为什么我能';是否在firestore数据库中添加任何字段?

Angular 为什么我能';是否在firestore数据库中添加任何字段?,angular,firebase,google-cloud-firestore,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', styleUrl

我的组件如下所示:

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"
      });

    }
}
从'@angular/core'导入{Component,OnInit};
从“rxjs”导入{Observable};
从“angularfire2/firestore”导入{AngularFirestore};
@组成部分({
选择器:“应用程序任务”,
templateUrl:“./mission.component.html”,
样式URL:['./mission.component.css']
})
导出类任务组件{
公共物品:可见;
建造商(db:AngularFirestore){
this.items=db.collection('/todos').valueChanges();
}
日志格式(值:任意){
console.log(值);
此.db.collection(“/todos”).add({
名称:“洛杉矶”,
国家:“CA”,
国家:“美国”
});
}
}
但是我不明白为什么我在
this.db方面遇到问题,每次我都会遇到这个错误:
属性“db”在类型“MissionComponent”上不存在


可能我在这里遗漏了一些东西。

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
    }
导出类任务组件{
公共物品:可见;
私有数据库:AngularFirestore;//这将是this.db
建造商(db:AngularFirestore){
this.items=db.collection('/todos').valueChanges();
this.db=db;//现在可以使用this.db
}
Try:
constructor(私有数据库:AngularFirestore)