0

I am fetching ratings of a particular post and then after that iterating over each reviewer, get their imageId and then fetching the image from backend using getImage() (defined in my services) and then using file reader assigning it to a variable and then displaying in the app.component.html file.But the problem is that when I am rendering this image in app.component.html file then I am getting the same image for all the reviewers.

Here is the code for fetching and iterating over each reviewer and then fetching images using imageId of each reviewer:-

```this.locolService.getPlaceRating(this.placeId).subscribe((data) => {
          this.ratings = data; console.log(this.ratings);
          this.ratings.forEach(rating => {
            this.locolService.getImage(rating.reviewer.profilePictureId).subscribe((image) => {
              let reader = new FileReader();
              reader.addEventListener("load", () => {
                this.profileImageOfReviewer = reader.result; // here 
                  assigning the value for displaying

              }, false);
              if (image) {
                reader.readAsDataURL(image);
              }
            })
          });
        });```


Here is the code that I have used to display images in app.component.html file.
```<div *ngFor="let rating of ratings">
        <div class="row">
          <div class="col-md-2">
            <img [src]="profileImageOfReviewer | safe" class="img-fluid rounded-circle">
          </div>
          <div class="col-md-10">
            <p class="highlight mb-0">
              <b>{{rating.reviewer.fullName}}</b> -
             <span class=" " *ngIf="recommended"><i class="material-icons orange600">
                 thumb_up_alt
               </i></span>
             <span class=" " *ngIf="!recommended"><i class="material-icons orange600">
                 thumb_down_alt
               </i></span>
            </p>
            <p class="mb-0  ">
              <span class=" "> {{rating.month }} - {{rating.year}}</span>
            </p>
            <p class="mb-0  ">{{rating.reviewText}}</p><br>
            <ngb-rating [rate]="rating.ratingValue"></ngb-rating>
          </div>
        </div>
        <hr>
      </div>```

It should render images of all the reviewer

5
  • What is a console.log of profileImageOfReviewer ? It is a array of images? Commented May 20, 2019 at 6:52
  • There is a very big binary data coming in the console log. It's only a single image. Commented May 20, 2019 at 7:30
  • 1
    If you are getting a single image then inside the *ngFor same image is iterate. so if we have a multiple image then we are map that imageArray to ratings array and iterate different image for each iteration. Commented May 20, 2019 at 7:42
  • Great @YashRami, This was a great trick for me. Thanks a lot, man, I searched a lot but did not find any answer, but your answer i.e to map the images to ratings did the trick. Thanks again. Commented May 20, 2019 at 12:05
  • Welcome :). I just post the answer it would be great if you accept the answer. Commented May 20, 2019 at 12:07

1 Answer 1

1

If you are getting a single image then inside the *ngFor same image is iterate. so if we have a multiple image then we are map that imageArray to ratings array and iterate different image for each iteration

Sign up to request clarification or add additional context in comments.

Comments

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.