【问题标题】:Angular Fire Store - Error "No Properties in common with type 'GetOptions'."Angular Fire Store - 错误“没有与类型 'GetOptions' 相同的属性。”
【发布时间】:2019-04-03 18:23:20
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: angular typescript google-cloud-firestore


    【解决方案1】:

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

    你需要调用类似的东西

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

    然后在你的 html 中你需要像这样使用异步管道

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

    如果您不想使用管道,您可以 .subscribe 可观察到的 valueChanges。喜欢..

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

    【讨论】:

      【解决方案2】:

      您没有将 getOptions 赋予 get 方法: 您必须提供一个 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

      【讨论】:

        猜你喜欢
        • 2020-06-19
        • 2018-05-14
        • 2019-03-13
        • 2021-08-09
        • 2019-09-09
        • 2023-01-05
        • 2020-02-22
        • 2022-07-05
        • 2017-11-27
        相关资源
        最近更新 更多