【问题标题】:TypeError: "warnInfo" is not iterableTypeError:“warnInfo”不可迭代
【发布时间】:2021-08-31 23:06:08
【问题描述】:

我收到这个错误:TypeError: "warnInfo" is not iterable 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)
}
}

【问题讨论】:

  • 我从来没有用过wio.db,但是获取的url不应该是number/${user.id}/${message.guild.id}

标签: javascript discord.js


【解决方案1】:

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

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

您必须使用 Object.keys 或 Object.entries 来迭代对象的属性或条目。

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);

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

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);

【讨论】:

  • for...in 循环也可以
猜你喜欢
  • 2019-04-01
  • 2018-08-06
  • 2021-11-05
  • 2013-09-01
  • 2017-08-27
  • 2018-10-10
  • 2021-12-13
  • 2019-02-20
  • 2020-03-27
相关资源
最近更新 更多