angular - Why I can't add any field in a firestore database with angular?
问题描述
I have my component that looks like this:
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"
});
}
}
but I don't understand why I have problems whit this.db
and every time I get this error: Property 'db' does not exist on type 'MissionComponent'
Probably I'm missing something here..
解决方案
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
}
推荐阅读
- python - Python:如何在调用父类时进行子类化?
- c++ - 回调问题错误:非静态数据成员的使用无效
- swift - 如何在 Watch Interface Builder 中表示页面点/面包屑/页面指示器?
- html - 如何在 Bootstrap NavBar 中将图像准确地放置在我想要的位置(并做出响应)
- java - 在 Java 中遇到“else if”问题,需要用户输入第二个文本示例来更改数据
- prolog - Prolog 反向查找和输入验证同时进行
- r - 使用 R 从 PDF 中抓取数据
- java - 如何在 Apache Nifi 上配置 HandleHttpRequest 以使用多个节点?
- linux - 错误标记大师:等待条件超时 [kubernetes]
- javascript - Javascript表单验证禁用提交没有任何框架