Angular Fire Store-错误“类型'GetOptions'没有共同的属性。”

时间:2019-04-03 18:23:20

标签: angular typescript google-cloud-firestore

我正在尝试从文档中获得一个价值。我尝试了以下方法:

getAuthorData(){
const test = this.afs.collection('Authors').doc('Test').get('name');
console.log(test);
}

不幸的是,我收到以下错误:

  

src / app / blogdetail / blogdetail.component.ts(55,65)中的错误:错误TS2559:“名称”类型与“ GetOptions”类型没有共同的属性。

2 个答案:

答案 0 :(得分:0)

基于这些文档-> https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md

您将需要拨打类似电话

const test = this.afs.doc('Authors/Test').valueChanges();

然后在您的html中,您需要使用异步管道,例如

{{ (item | async)?.name }}

如果不想使用管道,则可以订阅valueChanges观察值。喜欢。

this.afs.doc('Authors/Test').valueChanges().subscribe(doc => {
    this.name = doc.name;
}

答案 1 :(得分:0)

您没有为 get 方法提供 getOptions: 您必须提供一个 getOptions 对象,如此处 https://firebase.google.com/docs/reference/js/firebase.firestore.GetOptions 所述。

var getOptions = {
source: 'cache'
};

但我认为这不能解决您的问题,我想您想从文档中获取 name 的值。那么你应该尝试这样的事情:

this.afs.doc('Authors/'Test').get().then(object =>{
  test = object.get("name");
});

您可以在此处查看文档! https://firebase.google.com/docs/firestore/query-data/get-data