Skip to content
Advertisement

TypeError: Cannot read properties of undefined (reading ‘0’) from json.results

Error : TypeError: Cannot read properties of undefined (reading ‘0’) at Object.handler (C:WindowsSystem32C.C-Mdpluginscharainfo.js:7:70) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Object.handler (C:WindowsSystem32C.C-Mdhandler.js:515:25)

code :

let fetch = require('node-fetch')
let handler = async(m, { conn, text }) => {
  if (!text) throw `Masukkan query!`
  let res = await fetch(global.API('https://api.jikan.moe', '/v4/characters', { q: text }))
  if (!res.ok) throw await res.text()
  let json = await res.json()
  let { name, nicknames, url, image_url, about, type } = json.results[0]
let charaingfo = `💬 *Name:* ${name}
💭 *Nickname:* ${nicknames}
🔗 *Link*: ${url}
👤 *About*: ${about}`

  conn.sendFile(m.chat, image_url, '', charaingfo, m)
}
handler.help = ['character <nama>']
handler.tags = ['internet']
handler.command = /^(chara|character)$/i

module.exports = handler

Expected JSON Payload

{"pagination":{"last_visible_page":1,"has_next_page":false,"current_page":1,"items":{"count":1,"total":1,"per_page":25}},"data":[{"mal_id":417,"url":"https://myanimelist.net/character/417/Lelouch_Lamperouge","images":{"jpg":{"image_url":"https://cdn.myanimelist.net/images/characters/8/406163.jpg"},"webp":{"image_url":"https://cdn.myanimelist.net/images/characters/8/406163.webp","small_image_url":"https://cdn.myanimelist.net/images/characters/8/406163t.webp"}},"name":"Lelouch Lamperouge","name_kanji":"u30ebu30ebu30fcu30b7u30e5u30fbu30e9u30f3u30dau30ebu30fcu30b8","nicknames":["Lelouch vi Britannia","Zero","Lulu","Black Prince","The Demon Emperor"],"favorites":156830,"about":"Age: 17 (first season), 18 (second season)nDate of Birth:  December 5, 1999 a.t.b. (Sagittarius)nBlood Type: AnNationality: BritanniannHeight: 178 cm (5'10")nWeight: 56 kg (123 lbs)nOccupation: Student, Terrorist, Knightmare Pilot, Politician, Emperor, PrincenAllegiance: Order of the Black KnightsnPosition: Leader of the Black KnightsnKnightmare: Sutherland, Burai, Gawain, ShinkirounnLelouch vi Britannia (u30ebu30ebu30fcu30b7u30e5u30fbu30f4u30a3u30fbu30d6u30eau30bfu30cbu30a2, Ruru016bshu Vui Buritania) is the main protagonist of the anime/manga series Code Geass. He is the Eleventh Prince of the Holy Britannian Empire and the son of the 98th Emperor of Britannia, Charles zi Britannia.nnLelouch is a very clever individual who is also calm, sophisticated, and arrogant due to his aristocratic upbringing. While at school, Lelouch conducts himself as a friendly, likable, and often an easygoing student. However, this is a mask to hide his true nature. While as Zero, his true nature is expressed. His charisma and beliefs in justice gain him the trust and respect of many soldiers and leaders.nnHe is known for having a very stoic personality. He never cared about schoolwork, seeing the entire thing as trivial, even though his high intelligence would make it easy for him. He enjoys seeking out challenges, often playing chess against the nobility. In general, Lelouch takes most day-to-day affairs with open disinterest, often not even noticing the affection of others, especially Shirley, his classmate. He has a strong dislike for nobles, viewing them as tepid and "overprivileged parasites."nnMany characters have noted that Lelouch is entirely selfish, as his desire to remake the world into what he wants it to become from his desire to avenge his mother's apparent death and Nunnally's sake. However, in time, he realizes that this goal is not just for them but also the entire world.nnDespite his cold, calculating demeanor and ruthlessness in battle, he can be a rather compassionate person to his friends and loved ones. To Nunnally, he is a loving older brother, and to Suzaku, a loyal friend, although the two are enemies. Lelouch, at first glance, seems to have relatively little concern for the well-being of his subordinates, but in reality, he does care about them, seeing them as valuable allies.nnnCharacter Background (as Lelouch):nnCharacter Background (as Zero):nnFirst Season:nnSecond Season:nnnGeass:nLelouch's Geass, bestowed upon him by C.C., gives him "The Power of Absolute Obedience," allowing him to plant commands within a person's mind upon eye contact in a manner comparable to hypnosis. Activation of his Geass is visually represented by the manifestation of a Geass Sigil in his left eye. Commands dictated are written into the minds of the designated targets, the Sigil projecting him from his eye to theirs.nnOf all the Geass abilities in the series, Lelouch's has been explored the most and also seems to have the most restrictions, and side effects of any Geass introduced:(Source: Code Geass Wiki, Villains Wiki, Heroes Wiki, Anime and Manga Universe Wiki)"}]}

how to fix it, thanks

Advertisement

Answer

Ok given the JSON you expected, change how you access the data:

let { name, nicknames, url, image_url, about, type } = json.data[0]

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement