【问题标题】:Angular 5 - Observables: Can't access the value of a document using FirestoreAngular 5 - Observables:无法使用 Firestore 访问文档的值
【发布时间】:2019-02-03 12:06:16
【问题描述】:

当我尝试使用其 id 获取文档时,我无法获取文档的值,当我在控制台中显示变量时,它会显示

Observable {_isScalar: false, source: Observable, operator: MapOperator}

这是我的服务中我的功能的定义

getDataDetails(id: any) {
    this.dataDocumment = this.afs.doc('economie/' + id);
    return this.dataDocumment.valueChanges();;
  }

这是我的模板

<div class="main-content">
  <div class="container-fluid">
      <div class="header text-center">
          <h3 class="title">{{dataD.adresse | async | json}}</h3>
          <p class="category">Ensemble d'informations sur les communes prioritaires ou intervient CPDEP</p>
      </div>
      <div class="places-sweet-alerts">
          <div class="row">
              <div class="col-md-12">

                  <div class="card">
                      <div class="card-header card-header-icon" data-background-color="red">
                          <i class="material-icons">assignment</i>
                      </div>
                      <div class="card-content">
                          <h4 class="card-title">Présentations des communes</h4>
                          <div class="toolbar">



                                  <div class="col-lg-2"></div>
                                  <div class="col-lg-8">
                                      <div class="card">
                                          <div class="card-header card-header-danger" data-background-color="red">
                                              <h4 class="card-title">Circonscription de </h4>
                                              <p class="category">Département du </p>
                                          </div>
                                          <div class="card-content">
                                              <ul class="list-group list-group-flush">
                                                  <li class="list-group-item">Arrondissement : </li>
                                                  <li class="list-group-item">Nombre de sections communales : </li>
                                                  <li class="list-group-item">Organisation : </li>
                                                  <li class="list-group-item">Superficie : </li>
                                                  <li class="list-group-item">Population : </li>
                                                  <li class="list-group-item">Economie : </li>
                                                  <li class="list-group-item">Nombre de centres de vote : </li>
                                                  <li class="list-group-item">Nombre de bureaux de vote : </li>
                                                  <li class="list-group-item">Nombre d'électeur : </li>

                                              </ul>
                                          </div>
                                      </div>

                                  </div>
                                  <div class="col-lg-2"></div>
                              

                          </div>

                      </div>
                      <!-- end content-->
                  </div>
                  <!--  end card  -->
              </div>
              <!-- end col-md-12 -->
          </div>
      </div>
  </div>
</div>

这是我的组件代码

import { Component, OnInit } from '@angular/core';
import { EconomieService } from '../../services/economie.service';
import { Router, Params, ActivatedRoute} from '@angular/router';
@Component({
  selector: 'app-economie-detail',
  templateUrl: './economie-detail.component.html',
  styleUrls: ['./economie-detail.component.scss']
})
export class EconomieDetailComponent implements OnInit {
  id : string ;
  dataD : any;
  constructor(private eco: EconomieService,private route: ActivatedRoute) { }

  ngOnInit() {
    this.id = this.route.snapshot.params['id'];
    this.dataD = this.eco.getDataDetails(this.id);
  }
}

知道出了什么问题吗?

【问题讨论】:

  • 您需要使用.subscribe 在可观察对象发出时从其中获取数据。
  • 也许这个帖子可以帮助link
  • @ahsan,看起来它是针对旧版本的,因为它使用.then 来解开值。当前版本的 AngularFire2 返回一个Observable。无论如何,这里使用valueChanges()
  • 您的组件代码仍然丢失。
  • dataD.adresse | async | json 应该是 dataD | async | json 顺便说一句,这只是为了检查这个 Observable 将解析到的值。尝试这样做:(dataD | async)?.adresse

标签: angular observable angular-services


【解决方案1】:

根据AngularFire2 documentation,使用valueChanges() 的Stream Document Data 返回一个Observable,需要将其解包以获取实际数据。

您必须subscribe 才能获得价值。

您可能还想考虑简单地使用async 管道,以免订阅中的unsubscribe 避免任何内存泄漏。

因此,考虑到此函数是从名为 DataService 的服务中公开的,无论您在何处使用此函数,都可以这样做:

import { Component, Input } from '@angular/core';

@Component({
  ...
})
export class HelloComponent  {

  data;
  data$;

  constructor(private dataService: DataService) {}

  ngOnInit() {
    this.dataService.getDataDetails('someId').subscribe(data => console.log(data));

    // OR
    this.data$ = this.dataService.getDataDetails('someId');

  }

}

然后,在您的模板中:

{{ data$ | async | json }}

这将在通过 async 管道传递 Observable 时展开。无论async 管道的输出是什么,都将通过json 管道将stringify JSON 对象转换为字符串。这就是将打印到字符串上的内容。希望现在有意义。

针对您的特定用例

使用这个:

{{ (dataD | async)?.adresse }}

如果有帮助,这里是StackBlitz。我很确定这应该使它起作用。这是我在 Cloud Firestore 中拥有的那种数据。

【讨论】:

  • 我按照您的指示,现在它在控制台中显示我的文档的值,但绑定返回在我的模板中显示 null
  • 请为您的 Component TypeScript 类和模板发布一个最小的代码示例
  • 请确保只使用datadata$。如果您在模板中仅使用 data,请像使用 {{ data | json }} 一样使用它。如果您使用的是data$,请使用{{ data$ | async | json }}
  • 它不适用于我的情况,现在它给了我这个错误 InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe after your last recommendations
  • 只有在dataD 不是可观察对象而是对象时才有意义。在这种情况下,只需使用 {{ dataD | json }} 来查看打印到 DOM 的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 2021-03-19
  • 1970-01-01
  • 2021-06-08
  • 2020-12-17
  • 2018-11-24
相关资源
最近更新 更多