0

Got an error with quick.db

I tried to make a view-case command to view details of user's punishments and all that. But i got an error saying warnInfo is not iterable can someone help me

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

Error logs

(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..```
Roe Fino
  • 1
  • 1

1 Answers1

0

Normally that error shows up when you're accessing "things" that aren't "array-like", but that's fairly obvious. For debugging reasons, just add a console.log(warnInfo) or a debugger after this line: const warnInfo = database.get(`info.${message.author.id}.${message.guild.id}`).

That way you will see the type of value the db is sending back to you and you will be able to adjust your code to properly read it.

I also see that you have the execute function as an async function, but I don't see any await or promises inside that block of code. It might be worth fixing that as well.

Jean Costa
  • 800
  • 7
  • 10
  • warnInfo is undefined in console.log. I had a warnings command and had almost the same code with view-case command, its working properly. But for this command i only check one of the warnings and not all of them like the warnings command – Roe Fino Oct 31 '20 at 12:12
  • Then it seems to me that you're simply accessing something that doesn't exist in that path you're trying to access in the Database. So, to fix your issue, just add a conditional that checks if you actually got something back from your query to the DB. If you did, run your logic, if not, just return. And if you're meant to always be getting something back, then check the rest of your code – Jean Costa Oct 31 '20 at 13:55