簡體   English   中英

未處理的拒絕:TypeError:無法讀取未定義的屬性(讀取“0”)在嘗試打印 discord.js v14 中的 JSON object 時出錯

[英]Unhandled Rejection: TypeError: Cannot read properties of undefined (reading '0') getting error while trying to print JSON object in discord.js v14

我想從 data.json 中獲取名稱並將其發布在 discord 嵌入中。 例如,我嘗試在嵌入中打印Animal Skull ,但它給了我這個錯誤。

錯誤

Unhandled Rejection: TypeError: Cannot read properties of undefined (reading '0')

測試.js

const { EmbedBuilder } = require("discord.js"); 
const datapath = ('data.json')

module.exports = {
  config: {
    name: "test",
    description: "Just a test command!",
  },
  permissions: ['SendMessages'],
  owner: false,
  run: async (client, message, args, prefix, config, db) => {

    message.reply({ embeds: [
      new EmbedBuilder()
        .setDescription(datapath.names[0])
        .setColor("Green")
    ] })
    
  },
};

我的數據。json

[
    {
        "names": "Animal Skull",
        "itemtype": "Block, 32, 4, No, Yes",
        "image": "https://static.wikia.nocookie.net/cubixworlds/images/4/4c/Animal_Skull.png/revision/latest?cb=20220720212850"
    },
    {
        "names": "Asteroid Block",
        "itemtype": "Block, 1, 1, No, Yes",
        "image": "https://static.wikia.nocookie.net/cubixworlds/images/f/fe/Asteroid_Block.png/revision/latest?cb=20220725063800"
    }
]

如果有人可以幫助我,我將非常感激。

您應該使用require從 json 獲取數據

const datapath = require('./data.json')

你的 json 包含一個對象數組,所以使用datapath[0].names來獲取動物頭骨

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM