簡體   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