【问题标题】:When trying to access the "pricePerUnit" in the hypixel api, i get the error: TypeError: Cannot read property 'pricePerUnit' of undefined尝试访问 hypixel api 中的“pricePerUnit”时,出现错误:TypeError: Cannot read property 'pricePerUnit' of undefined
【发布时间】:2020-12-20 21:53:17
【问题描述】:

{"success":true,"lastUpdated":1598982272495,"products":{"BROWN_MUSHROOM":{"product_id":"BROWN_MUSHROOM","sell_summary":[{"amount":160,"pricePerUnit":13.9,"orders":1},{"amount":28503,"pricePerUnit":13.8,"orders":2},{"amount":71483,"pricePerUnit":13.4,"orders":3}, 这就是 api 所说的,所以我假设我可以直接得到“pricePerUnit。但我得到了错误TypeError: Cannot read property 'pricePerUnit' of undefined

我的代码是:```client.on("message", message => { 如果(message.author.bot)返回;

const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();

if (command === "bazaar") {
    let product = args[0];

    fetch(`https://api.hypixel.net/skyblock/bazaar/product?key=${key}`)
    .then(result => result.json())
    .then(({ BROWN_MUSHROOM }) => {
        // Log the player's username
        message.reply(BROWN_MUSHROOM.pricePerUnit)
    })

}

})```

有人知道怎么帮忙吗?

【问题讨论】:

    标签: javascript api discord.js


    【解决方案1】:

    尝试将您的最后一个块修改为:

    .then(({ products: { BROWN_MUSHROOM } }) => {
        // Log the player's username
        message.reply(BROWN_MUSHROOM.pricePerUnit)
    })
    

    因为BROWN_MUSHROOM 似乎在products 字段下。

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 2016-10-28
      • 2020-11-07
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 2023-03-15
      相关资源
      最近更新 更多