tags: JavaScript Maria DB Python

Pokemon DB Part 1
Pokemon DB Part 2
Pokemon DB Part 3
Pokemon DB Part 4
Pokemon DB Part 5

Now we will get the correct Pokemon

We will create another function - this one will use the SetID as well as our CardNo

//Fetch Data
async function fetchData() {
    try {
        SetID = await checkDupes()
        data3 = await tcgdex.fetch('sets', SetID, CardNo) 

Again we will use an async function with a try statement
To get to our Pokemon we will get this from the TCGdex by searching in sets with our SetID - remember this was sv03 or sv03.5 there are a bunch of others. The CardNo was already given by the user an now we will finally bring it to use.

With this we will receive a complete info of the card which will look something like this

{
  "category": "Pokémon",
  "id": "sv04-113",
  "image": "https://assets.tcgdex.net/de/sv/sv04/113",
  "localId": "113",
  "name": "Absol",
  "rarity": "Ungewöhnlich",
  "set": {
    "cardCount": {
      "official": 182,
      "total": 266
    },
    "id": "sv04",
    "logo": "https://assets.tcgdex.net/de/sv/sv04/logo",
    "name": "Paradoxrift",
    "symbol": "https://assets.tcgdex.net/univ/sv/sv04/symbol"
  },
  "variants": {
    "firstEdition": false,
    "holo": true,
    "normal": true,
    "reverse": true,
    "wPromo": false
  },
  "hp": 100,
  "types": [
    "Unlicht"
  ],
  "stage": "Basis",
  "attacks": [
    {
      "cost": [
        "Farblos"
      ],
      "name": "Bewusstes Ziehen",
      "effect": "Du kannst beliebig viele Karten aus deiner Hand auf deinen Ablagestapel legen, bis du 4 oder weniger hast. Ziehe so lange Karten, bis du 5 Karten auf deiner Hand hast."
    },
    {
      "cost": [
        "Farblos",
        "Farblos"
      ],
      "name": "Verstärkte Klinge",
      "effect": "Wenn an dieses Pokémon 1 Pokémon-Ausrüstung angelegt ist, fügt diese Attacke 60 Schadenspunkte mehr zu.",
      "damage": "20+"
    }
  ],
  "retreat": 1,
  "regulationMark": "G",
  "legal": {
    "standard": false,
    "expanded": false
  }
}

As with every other thing in programming we need to catch any error in this use-case it could be one of the following

  • CardNo ist not in the specified set
  • Set is not filed in TCGDex - can happen with brand new Sets
  • Set does not exist (we should be covering this in the function before)

In any case this will lead to a undefined variable data3

        //Check if we Card is already in TCGdex        
        if (data3 !== undefined) {
            //Get Pokemon Name
            PokeName = data3.name
            //Get Rarity of Card
            rarity = data3.rarity
            //Get SetID
            SetID = data3.set.id
            //Get Set Name
            SetID_Name = data3.set.name
            //Inform User
            console.log(`Set ID ist ${textColor}${SetID}${resetColor}`)
            console.log(`Set Name ist ${textColor}${SetID_Name}${resetColor}`)
            console.log(`Unser Pokemon ist ${textColor}${PokeName}${resetColor}`)
            console.log(`Unser Pokemon ist ${textColor}${rarity}${resetColor}`)

            return data3
        //Return Error if Set does NOT contain DATA
        } else {
            console.log(SetID,'ist leider nicht befüllt')
        } 
    } catch (error) {
        console.error('Error fetching sets:', error);
        return null;
    }}

We are using the if statement to check that data3 is defined - actually is NOT undefined
Afterwards we will use some of the data of the JSON to write back in to the console.
We are using

  • Pokemon Name
  • The rarity
  • Which SetID we have (the stand sv03or in the above mentioned JSON sv04
  • The Set Name - which will give the correct name of the set used

All these values we will print out to the console and return the complete JSON in data3to the next function

console