【问题标题】:Ionic Angular Firestore not displaying single document data in viewIonic Angular Firestore 不在视图中显示单个文档数据
【发布时间】:2020-04-09 12:35:46
【问题描述】:

我正在尝试检索集合中的单个文档并将其显示在视图中,但它总是导致空白页。

我有一个服务和一个组件。

这是我的服务:

getReview(reviewID) {
      console.log('review id sent to service: ' + reviewID);
      this.reviewDoc = this.af.doc(`reviews/${reviewID}`);
      return this.reviewDoc.valueChanges();
  }

这是我的组件:

    ngOnInit() {
    const reviewID = this.route.snapshot.params.id;
    this.getReview(reviewID);
    }

    getReview(reviewID) {
    this.reviewDetailsService.getReview(reviewID).subscribe((data) => {
      this.review = data;
      console.log(this.review);
    });
    }

控制台日志显示: {imgURL: "http://someimage.jpg",caption: "This is my caption}

在我的 HTML 视图中,我正在这样做:

    <ion-content class="ion-padding">
    <div *ngFor="let rev of review">
        {{rev.caption}}
    </div>
    </ion-content>

这不会在页面上显示任何内容。

我错过了什么吗?

【问题讨论】:

    标签: angular google-cloud-firestore ionic4


    【解决方案1】:

    您不能在对象上使用 ngFor,在集合上使用 ngFor,请将您的视图更改为

     <div>
        <h1> {{review.caption}} </h1>
     </div>
    

    【讨论】:

    • 我现在收到这个错误:ERROR TypeError: Cannot read property 'caption' of undefined
    • 在您的回答的帮助下,我发现我还需要通过像这样review: any = [];review: any = []; 声明评论来创建我的评论属性和数组
    猜你喜欢
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多