【问题标题】:TypeError: json.find is not a function (discord.js)TypeError:json.find 不是函数(discord.js)
【发布时间】:2020-12-07 16:08:02
【问题描述】:

我尝试找到解决问题“json.find”以显示商品和价格 例如 输入.shop basic kibble

机器人回复Name: Basic Kibble cost: k50

或输入.shop food 机器人回复Name:Food cost k10

在 bot.js 中

bot.on("message", ({ author, channel, content }) => {
  if (author.bot || !content.startsWith(prefix)) return;

  const args = content.slice(prefix.length).split(" ");
  const command = args.shift();

  if (command === "shop") {
    if (!args.length) return message.channel.send("You must specify an item!");

    const input = args.join(" ");
    const items = json.find((object) => object.name === `item`);
    if (!items) return channel.send(`${input} isn't a valid item!`);

    channel.send(`Name: ${items.name} Cost: k${items.cost}`);
  }
});

【问题讨论】:

  • 在 shop.json 文件中``` {"item": [ { "name": "Basic Kibble", "cost": 50 }, { "name": "food", "cost ": 10 } ] }```
  • 您是否需要或导入过 json 文件?
  • 你在哪里定义“json”
  • const json = require("./data/shop.json");在 bot.js 的顶部
  • 试试json.JSON().find()

标签: arrays json find discord.js


【解决方案1】:

您在评论中说了以下内容: "在 shop.json 文件中 {"item": [ { "name": "Basic Kibble", "cost": 50 }, { "name": "food", "cost": 10 } ] }"

如果json是这个,就说明它是一个对象。您需要使用:

const items = json.item.find(object => object.name === `item`)

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2020-08-23
    • 2020-01-22
    • 2020-11-13
    • 2017-11-28
    • 2021-06-15
    • 2020-12-22
    • 2021-01-27
    • 2021-06-17
    相关资源
    最近更新 更多