This simple app is supposed to randomly chose a card and display its data.
I have these errors I am trying to fix without success :
Error 1 :
Typescript Error
Type ‘undefined[]’ is not assignable to type ‘Card’. Property ‘id’ is missing in type ‘undefined[]’.
Error 2 :
Type ‘{ “id”: string; “question”: string; “answer”: string; “hint”: string; “displayed”: string; }[]’ is not assignable to type ‘Card[]’. Type ‘{ “id”: string; “question”: string; “answer”: string; “hint”: string; “displayed”: string; }’ is not assignable to type ‘Card’. Types of property ‘displayed’ are incompatible. Type ‘string’ is not assignable to type ‘boolean’.
random-cards.ts
import {Component, OnInit} from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import cards from '../../providers/data/cards';
import {Card} from "../../providers/Models/cards.interface";
@Component({
selector: 'page-random-cards',
templateUrl: 'random-cards.html',
})
export class RandomCardsPage implements OnInit {
cards: Card[];
availableCards: Card[];
randomCard: Card;
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.cards = [];
this.randomCard = [];
this.availableCards = [];
}
ngOnInit() {
this.cards = cards.cards;
this.cards.map(card => card.displayed = false);
}
ionViewDidLoad() {
console.log('ionViewDidLoad RandomCardsPage');
console.log(this.cards);
}
getRandom() {
if (this.availableCards.length === 1) {
window.alert('Oops, that was our last Card !');
}
else
this.availableCards = this.cards.filter(card => !card.displayed);
let rd = Math.floor(Math.random() * this.availableCards.length);
this.randomCard = this.availableCards[rd];
this.cards.find(card => card.id === this.randomCard.id).displayed = true;
console.log(this.cards);
}
}
random-cards.html
<ion-navbar>
<ion-title>random-cards</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-grid>
<ion-row>
<ion-col>
<button ion-button (click)="getRandom()">Get Random</button>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-list>
<ion-item>
({{randomCard.id}}) {{randomCard.question}} - {{randomCard.answer}} - {{randomCard.displayed}}
</ion-item>
</ion-list>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
cards.interface.ts
export interface Card {
id: string;
question: string;
answer: string;
hint: string;
displayed: boolean;
}
cards.ts
export default
{
"cards": [
{
"id": "1",
"question": "question 1",
"answer": "answer 1",
"hint": "hint 1",
"displayed": "",
},
{
"id": "2",
"question": "question 2",
"answer": "answer 2",
"hint": "hint 2",
"displayed": "",
},
{
"id": "3",
"question": "question 3",
"answer": "answer 3",
"hint": "hint 3",
"displayed": "",
}
]
};