0

I am working on the Angular application with Strapi, and I am not able to get the output from Strapi.

This is my HTML page for the application:

<h1>Tour Events</h1>
<table *ngIf="events$ ">
    <tr>
        <th>Date</th>
        <th>Venue</th>
        <th>City</th>
        <th>Time</th>
        <th>Tickets</th>
    </tr>
    <tr *ngFor="let event of events$">
        <td>{{event.date | date: 'fullDate'}}</td>
        <td>{{event.venue }}</td>
        <td>
            <span *ngIf="event.region">{{event.city | titlecase}}, {{event.region | uppercase}}              ({{event.country |
                uppercase}})</span>
            <span *ngIf="!event.region">{{event.city | titlecase}} ({{event.country | uppercase}})</span>
        </td>
        <td>{{event.date | date: 'shortTime'}}</td>
        <td><a href="{{event.ticketsLink}}">Tickets</a></td>
    </tr>
</table>

This is the ts file:

import { Component, OnInit } from '@angular/core';
import { TourEventsService } from 'src/app/core/services/tour-events.service';
@Component({
  selector: 'app-tour-events',
  templateUrl: './tour-events.component.html',
  styleUrls: ['./tour-events.component.css']
})
export class TourEventsComponent implements OnInit{
  events$: any;
  constructor(private eventService: TourEventsService) { }
  ngOnInit(){
    this.events$ = this.eventService.getEvents().subscribe(()=>this.events$);


  }

}

service.ts:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { TourEvent } from '../models/tour-event';

@Injectable({
  providedIn: 'root'
})
export class TourEventsService {
  private eventsPath = 'api/tour-events';

  constructor(private http: HttpClient) { }

  getEvents(){
    return this.http.get<TourEvent[]>(environment.apiUrl+this.eventsPath);
  }
}

I tried to change the ngFor in the HTML file, but that is the error:

Cannot find a differ supporting object '[object Object]' of type 'object' NgFor only supports binding to Iterables, such as Arrays

I could not understand what was the error so I could not change anything. Can anyone help me?

3
  • Can you share the sample data in JSON? Thanks. Commented Jan 24, 2023 at 5:14
  • let me know the console value of this.events$. (eg. console.log(this.events$); Commented Jan 24, 2023 at 5:18
  • The return type of this.eventService.getEvents().subscribe(()=>this.events$); is a Subscription. Normally you use that variable to unsubscribe() when you want that the Observable stops emitting. Commented Jan 24, 2023 at 5:36

1 Answer 1

1

In Angular, a variable with the $ suffix represents an Observable. You should assign the Observable returned from eventService.getEvents method without the .subscribe event.

import { Observable } from 'rxjs';

events$: Observable<TourEvent[]>;

ngOnInit() {
  this.events$ = this.eventService.getEvents();
}

While you need the AsyncPipe in the view to subscribe to the events Observable.

<tr *ngFor="let event of events$ | async">
  ...
</tr>

Demo @ StackBlitz

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.