微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeError:“ x”不可迭代

如何解决TypeError:“ x”不可迭代

使用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..```

解决方法

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

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

我还看到您具有execute的{​​{1}}函数,但是在该代码块中看不到任何等待或承诺。也许也应该修复它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。