对于循环不开火 - Javascript

时间:2017-04-23 19:42:24

标签: javascript json for-loop fs

无论出于何种原因,我的for循环都没有触发。该命令应该返回所提到的用户的命令。 !commands @rusty会触发我的命令。 (JSON文件不为空,第6行工作正常。我的JSON文件如下所示:https://hastebin.com/awezatexob.json(我不能在此处发布此代码。

fs.readFile(path.join(__dirname, "../jsonFiles") + "/customCommands.json", "utf-8", function(err, data) {
  if (err) throw err;

  var arrayOfObjects = JSON.parse(data);

  if (arrayOfObjects.commands.length === 0) return message.reply("No custom commands found.");

  for (let i = 0; i > arrayOfObjects.commands.length; i++) {
    console.log("Hello"); // does not fire
    if (message.guild.id !== arrayOfObjects.commands[i].guild_id) return message.reply("No commands in guild.");
    if (user.id !== arrayOfObjects.commands[i].user_id) return message.reply(user.username + " has no commands in this guild.");

    fs.writeFile(path.join(__dirname, "../jsonFiles") + "/customCommands.json", JSON.stringify(arrayOfObjects, null, 2), "utf-8", function(err) {
      if (err) throw err;
      const embed = new Discord.RichEmbed()
      .setColor(0x08F258)
      .setAuthor("Custom Commands for " + user.username, message.author.avatarURL)
      .addField(arrayOfObjects.commands[i].command_name, arrayOfObjects.commands[i].command_reply)
      return message.channel.sendEmbed(embed);
    });
  }
});

2 个答案:

答案 0 :(得分:0)

i > arrayOfObjects.commands.length;

应该是

i < arrayOfObjects.commands.length;
IE:你的情况已经逆转了。

答案 1 :(得分:0)

在您的import ijson import pandas as pd with open('MY JSON FILE', 'r') as f: objects = ijson.items(f, 'events.item') pandaReadable = list(objects) df = pd.DataFrame(pandaReadable, columns=['userId', 'customParameters']) df.to_csv('C:/Users/.../Desktop/output.csv', columns=['userId', 'customParameters'], index=False) 论据中,您说for必须大于i,而它应该是相反的,就像这样

arrayOfObjects.commands.length

因此,当for (let i = 0; i < arrayOfObjects.commands.length; i++) 为真时,你的for循环将会运行