Я пытаюсь добавить информацию о среднем количестве отзывов, когда люди просматривают количество звезд, а затем подсчитывают, что mov ie имеет это среднее количество звезд. Я не знаю, как получить свой средний счет в html.
Вот мой HTML файл:
<div *ngFor="let movie of movieData">
<table class="table table-bordered">
<thead>
<tr>
<th>Reviewer</th>
<th>Stars</th>
<th>Reviews</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let info of movie.reviews">
<td>{{info.name}}</td>
<td>{{info.stars}}</td>
<td>{{info.comment}}</td>
</tr>
</tbody>
</table>
</div>
<p>Average rating: <b> {{avgRating}} </b></p>
мой компонент DetailsComponent:
export class DetailsComponent implements OnInit {
@Input() avgRating: any;
movieID: string;
movieData: any;
avg;
constructor(
private router: Router,
private route: ActivatedRoute,
private httpService: HttpService
) {}
ngOnInit() {
this.movieID = this.route.snapshot.paramMap.get("id");
this.getOneMovie();
}
getOneMovie() {
let observable = this.httpService.oneMovie(this.movieID);
observable.subscribe((data) => {
this.movieData = data;
});
}
getMovie(idx) {
var sum = 0;
for (var i = 0; i < this.movieData.reviews.length; i++) {
sum += this.movieData.reviews[i].stars;
}
this.avg = sum / this.movieData.reviews.length;
}
}
и моя модель. js file:
const ReviewSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Customer name is required"],
minlength: [3, "Customer name must be a minimum of 3 characters"]
},
stars: {
type: Number,
required: [true, "Star is required"]
},
comment: {
type: String,
required: [true, "Review is required"],
minlength: [3, "Review must be a minimum of 3 characters"]
},
})
const MovieSchema = new mongoose.Schema({
title: {
type: String,
required: [true, "Movie title is required"],
minlength: [3, "Movie title must be a minimum of 3 characters"]
},
reviews: [ReviewSchema]
})
Кто-нибудь может помочь?