【问题标题】:TypeError: 'x' s not iterable类型错误:“x”不可迭代
【发布时间】:2021-02-13 16:27:09
【问题描述】:

quick.db 出错

我试图创建一个 view-case 命令来查看用户惩罚的细节等等。但我收到一条错误消息,提示 warnInfo is not iterable 有人可以帮我吗

const Discord = require("discord.js");
const database = require("quick.db");
const config = require('../config.json');

module.exports = {
    name: "view-case",
    description: "View details of a user's punishment / warnings.",
    async execute(message, args) {
    const warnInfo = database.get(`info.${message.author.id}.${message.guild.id}`)

    if(!args.length) {
// invalid usage returns
    };

    for(let warnings of warnInfo){
        let mod = warnings.moderator
        let reason = warnings.reason
        let date = warnings.date
        let cases = warnings.cases
        let numbercase = warnings.numbercase
        let dur = warnings.duration
    
        if(args[0] === numbercase) {
            let embed = new Discord.MessageEmbed()
            .setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
            .setTitle(`Case #${numbercase}`)
            .setDescription(`**Case**: ${cases} \n**Moderator**: ${mod} \n**Reason**: ${reason} \n**Date**: ${date} \n**Mute Duration**: ${dur}`)
            .setColor("RANDOM")
            .setTimestamp();
            return message.channel.send(embed)
        }
    }
    }
}

错误日志

(node:3308) UnhandledPromiseRejectionWarning: TypeError: warnInfo is not iterable
    at Object.execute (C:\Users\DS9\Documents\GitHub\sarah\commands\view-case.js:29:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:3308) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3308) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with
a non-zero exit code..```

【问题讨论】:

  • 您检查过warnInfo 是什么吗?它是可迭代的吗?

标签: javascript node.js discord.js node-sqlite3 quick.db


【解决方案1】:

通常,当您访问不是“类数组”的“事物”时会出现该错误,但这很明显。出于调试原因,只需在此行之后添加console.log(warnInfo)debuggerconst warnInfo = database.get(`info.${message.author.id}.${message.guild.id}`)

这样您将看到 db 发送回给您的值的类型,并且您将能够调整您的代码以正确读取它。

我还看到您将execute 函数用作async function,但我在该代码块中看不到任何等待或承诺。可能也值得修复它。

【讨论】:

  • warnInfo 在 console.log 中未定义。我有一个警告命令,并且与 view-case 命令的代码几乎相同,它工作正常。但是对于这个命令,我只检查一个警告,而不是像警告命令一样检查所有警告
  • 那么在我看来,您只是在访问您试图在数据库中访问的路径中不存在的东西。因此,要解决您的问题,只需添加一个条件来检查您是否真的从查询中将某些内容返回到数据库。如果你这样做了,运行你的逻辑,如果没有,就返回。如果你打算总是得到一些东西,那么检查你的代码的其余部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2012-10-30
  • 2022-11-29
  • 2017-03-05
  • 2016-03-10
  • 2021-03-02
  • 2020-04-18
相关资源
最近更新 更多