I received a basic JSON response containing a single object. My intention was to display this on an HTML page, but unfortunately I encountered the following error:
src/app/games/game.component.ts:35:7 - error TS2740: Type 'never[]' is missing the following properties from type 'GAMEDETAIL': id, title, releasedate, description, and 6 more
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
interface GAMEDETAIL {
id: number,
title: string
releasedate: number
description: string,
adddate: string,
changedate: string,
pdffile: string,
youtubelink: string,
images: any,
producer: any
}
@Component({
selector: 'app-game',
templateUrl: './game.component.html',
styleUrls: ['./game.component.css']
})
export class GameComponent implements OnInit {
private data:any = [];
gamedetail:GAMEDETAIL;
gameid: number = 0;
constructor(private route: ActivatedRoute, private http:HttpClient) {
**this.gamedetail = [];**
}
ngOnInit(): void {
this.gameid = parseInt( this.route.snapshot.paramMap.get('id') as string );
this.getJSON(this.gameid).subscribe(data => {
console.log(this.gamedetail.title);
});
}
getJSON(spielid: Number): Observable<GAMEDETAIL[]> {
return this.http.get<GAMEDETAIL[]>("https://example.org/api/gamedetail/" + spielid);
}
}
Upon checking the console, I found the following JSON Data:
{
"id":1,
"title":"Ligretto",
"releasedate":2000,
"producer":[
{
"company":"Schmidt",
"url":"https://example.org"
}
],
"pdffile":"https://example.org",
"discription":"This is the game's description",
"images":[
{
"position":0,
"imagefile":"https://example.org"
},
{
"position":1,
"imagefile":"https://example.org"
}
],
"youtubelink":"https://example.org",
"adddate":"2021-12-22 22:22:44",
"changedate":"2022-01-11 11:11:20"
}
I attempted to log the line below using the console:
console.log(this.gamedetail.title);
However, I encountered issues while trying to compile it.