Problem pulling data from NASA API service

Data comes from the NASA API service. But I can’t list them. Can someone help me?

My code;


import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

  providedIn: 'root'
export class NasaService {
  public apiUrl: any = '';
  public apiKey: any = '?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

  constructor(private http: HttpClient) { }

  getImage() {
    console.log('Image Loaded');
    const url = this.apiUrl + this.apiKey;
    return this.http.get(url);

import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
import { NasaService } from '../nasa.service';

  selector: 'app-tab3',
  templateUrl: '',
  styleUrls: ['']
export class Tab3Page {

  data: any = [];

  constructor(private nasaService: NasaService, private http: HttpClient, private router: Router) {}

  ngOnInit() {
    .subscribe(data =>{
      console.log(data); = data;

      Astronomy Picture of Day

    <ion-item *ngFor="let result of data.{I don't know what to write here.}">
      <ion-img [src]="result.url"></ion-img>

API Result


I think it’s worth the time and effort to define interfaces for everything in your app. You should almost never need any in an Ionic app, to the point where it should make you somewhat physically ill when you type that word.

interface PictureOfTheDay {
  service_version: "v1" | "v2";
  copyright?: string;
  date: Date;
  title: string;
  explanation: string;
  media_type: "image" | "video";
  url: string;
  hdurl: string;

(the declarations of service_version and media_type are arbitrary, intended to show how to write enums in TypeScript)

Now, you can write:

getImage(): Observable<PictureOfTheDay> {
  return this.http.get<PictureOfTheDay>(url);
potd$: Observable<PictureOfTheDay>;

constructor(nasaService: NasaService) {
  this.potd$ = nasaService.getImage();

Now your IDE can take over and tell you what to write in the template (no ngFor, because you aren’t getting an array, as far as I can see):

<ng-container *ngIf="(potd$ | async) as potd">
    <h1 [innerText]="potd.title"></h1>
    <ion-img [src]="potd.url"></ion-img>
    <h4 [innerText]=""></h4>
    <ion-label [innerText]="potd.explanation"></ion-label>
1 Like

Can you tell me where to write this codes?
(Note: I am writing with Angular.)