I want to create a class Deck that gets two attributes id and remaining. I use the Deck of Cards API to get the information of a deck but every time I create a Deck I keep getting id and remaining as undefined and I don’t understand why because the JSON value is good and when I print it I got something but my attributes are undefined.
class Deck {
constructor(numberOfDeck, jokersEnabled) {
let url;
if (jokersEnabled) {
url = "https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=" + numberOfDeck + "&jokers_enabled=true";
} else {
url = "https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=" + numberOfDeck;
}
console.log(url);
fetch(url).then(res => res.json()).then(value => {
this.id = value.deck_id;
this.remaining = value.remaining;
}).catch(function(err) {
console.log(err);
});
}
}
let deck = new Deck(1, true);
console.log(deck.id + " " + deck.remaining);