繁体   English   中英

TypeError:“warnInfo”不可迭代

[英]TypeError: “warnInfo” is not iterable

伙计们,我收到此错误:TypeError:“warnInfo”不可迭代 db.get 无法正常工作,因为我的数据库与 db.get 不兼容,还有其他解决方案吗?

const Discord = require("discord.js")
const db = require("wio.db")
module.exports = {
  kod: "warns",
  async run (client, message, args) {
    let user;
    if(!args[0]) user = message.author
    if(args[0] && isNaN(args[0])) user = message.mentions.users.first()
    if(args[0] && !isNaN(args[0])){
        user = client.users.cache.get(args[0])

        if(!message.guild.members.cache.has(args[0])) return message.reply(":x: User not found.")

    }
    if(!user) return message.reply(":x: You must tag a user")

    const number = db.fetch(`number.${user.id}.${message.guild.id}`)
    const warnInfo = db.fetch(`info.${user.id}.${message.guild.id}`)

if(!number || !warnInfo || warnInfo == []) return message.reply("Doesn't have warn")
const warnembed = new Discord.MessageEmbed()

for(let warnings of warnInfo){
    let mod = warnings.moderator
    let reason = warnings.reason
    let date = warnings.date

warnembed.addField(`${user.tag} warns`,`**Moderator:** ${mod}\n**Reason:** ${reason} \n**Date:** ${date}\n**Warn ID:** \`${warnings.id}\``,true)
}
warnembed.setColor(message.guild.members.cache.get(user.id).roles.highest.color)

message.channel.send(warnembed)
}
}

在 JavaScript 中,对象是不可迭代的,除非它们实现了可迭代协议。 因此,您不能使用 for...of 来迭代 object 的属性。

var obj = { 'France': 'Paris', 'England': 'London' };
for (let p of obj) { // TypeError: obj is not iterable
    // …
}

相反,您必须使用 Object.keys 或 Object.entries 来迭代 object 的属性或条目。

var obj = { 'France': 'Paris', 'England': 'London' };
// Iterate over the property names:
for (let country of Object.keys(obj)) {
    var capital = obj[country];
    console.log(country, capital);
}
for (const [country, capital] of Object.entries(obj))
    console.log(country, capital);

此用例的另一个选项可能是使用 Map:

var map = new Map;
map.set('France', 'Paris');
map.set('England', 'London');
// Iterate over the property names:
for (let country of map.keys()) {
    let capital = map[country];
    console.log(country, capital);
}

for (let capital of map.values())
    console.log(capital);

for (const [country, capital] of map.entries())
    console.log(country, capital);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM