1

I am trying to retrieve a single document in a collection and display it in a view, but it always results in a blank page.

I have a service and a component.

Here is my service:

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

Here is my component:

    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);
    });
    }

The console log shows this: {imgURL: "http://someimage.jpg",caption: "This is my caption}

In my HTML view I am doing this:

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

This does not display anything on the page.

Am I missing something?

1 Answer 1

1

You cannot use ngFor over an object, ngFor used over a collection, change your view as

 <div>
    <h1> {{review.caption}} </h1>
 </div>
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error now: ERROR TypeError: Cannot read property 'caption' of undefined
With the help of your answer I was able to find out that I needed to also make my review property and array by declaring review like this review: any = [];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.