0

So i recently made this code with a warn command in discord js but there's a problem my if statement doesn't work like i wanted it to be and i wanted it to be if value doesn't exists return message.channel.send("This user has no records!"); Because i never tried using sqlite on javascript and i only know sqlite on c#

const con = require("../../sql.js");
con.all(`SELECT * FROM warnings WHERE username = ? AND userID = ?`, User.tag, User.id, (err, rows) => {
    rows.forEach(r => {
        if (!rows) {
            message.channel.send("NO U");
        }
        else {
            let moderator = (r.moderator);
            let modID = (r.modID);
            let reason = (r.reason);
            let time = (r.time);

            let Avatar = User.displayAvatarURL();

            let Embed = new MessageEmbed()
                .setTitle(`User warnings!`)
                .setDescription(
                    `These is all the warnings which have been issued to ${User.tag}!`)
                .setColor(`RED`)
                .setThumbnail(Avatar)
                .addFields({
                    name: "Moderator",
                    value: `${moderator}`,
                    inline: true
                }, {
                    name: "Username",
                    value: `${User.tag}`,
                    inline: true
                }, {
                    name: "Reason",
                    value: `${reason}`,
                    inline: true
                }, {
                    name: "When was warned",
                    value: `${time}`,
                    inline: true,
                });

            message.channel.send(Embed).then(msg => {
                msg.delete({
                    timeout: 15000
                });
                message.delete({
                    timeout: 3000
                });
            })
        }
    })
})
1
  • 1
    Testing (!rows) inside the .forEach() doesn't make sense; if rows is null or undefined your code would never make it inside the .forEach(). Commented Aug 5, 2020 at 12:22

2 Answers 2

1

Move the if statement outside of the forEach loop.

Before:

rows.forEach(r => {
  if (!rows) {
      message.channel.send("NO U");
  }

After:

if (rows.length === 0) {
    return message.channel.send("NO U");
}
rows.forEach(r => {
                   

As pointed out in other comments, change !rows to rows.length === 0 since the library returns an empty array when no rows are found.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok i'm actually gonna test this
@DoggoLeaker Not sure if you are being serious or not... like Pointy said, you can't test a null/undefined variable inside a loop on that variable, as it will never be hit
@nip Empty arrays are truthy in JavaScript; you'd need !rows.length at the very least.
@AKX that depends on what rows is. I'm not familiar with the library he's using, so I assumed the condition was fine. In that case, change to .length === 0
0

I'd refactor things so there's a separate function that turns an user and the row from the database into a MessageEmbed.

That way it's easy to first try and map any warnings there are to messages, then look at whether any got generated, and if not, send out the other message.

Note that even empty arrays are truthy in JavaScript, so you'll need to check for .length.

const con = require("../../sql.js");

function warningRowToMessage(User, row) {
  const { moderator, reason, time } = row;
  return new MessageEmbed()
    .setTitle(`User warnings!`)
    .setDescription(`These is all the warnings which have been issued to ${User.tag}!`)
    .setColor(`RED`)
    .setThumbnail(User.displayAvatarURL())
    .addFields(
      {
        name: "Moderator",
        value: `${moderator}`,
        inline: true,
      },
      {
        name: "Username",
        value: `${User.tag}`,
        inline: true,
      },
      {
        name: "Reason",
        value: `${reason}`,
        inline: true,
      },
      {
        name: "When was warned",
        value: `${time}`,
        inline: true,
      },
    );
}

con.all(`SELECT * FROM warnings WHERE username = ? AND userID = ?`, User.tag, User.id, (err, rows) => {
  const replyMessages = rows.map((r) => warningRowToMessage(User, r));
  if (!replyMessages.length) {
    message.channel.send("NO U");
    return;
  }
  message.delete({
    timeout: 3000,
  });
  replyMessages.forEach(async (replyMessage) => {
    const msg = await message.channel.send(replyMessage);
    msg.delete({
      timeout: 15000,
    });
  });
});

3 Comments

There's a problem now It works but when the user has warnings it doesn't display in else...
@AKXX new code : sourceb.in/4d63ca6b63 but still doesn't work
New code also : sourceb.in/44758871c6 when the row is found it sends bought of them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.