1

I am working on a bot using discord.js. My goal requires me to leverage SQL to store user information and their points. When I try to use the !points command, I receive a correct response from my console. My !points case statement is however getting back an undefined value. Upon testing without the if statement, I get a message back like this: has undefined points.

bot.on("message", function(message) {
  if (message.author.equals(bot.user)) return;

  var command = message.content.split(" ")[0];
  var param = message.content.split(" ")[1];

  if (!VALID_CMDS.includes(command)) return;
  var GUILD = bot.guilds.values().next().value;
  switch (command) {
    case "!hello":
      if (param) {
        message.channel.send("Hello, " + param);
      } else {
        message.channel.send("Use a second parameter and try again. FORMAT: !hello <name_goes_here>");
      }
      break;
    case "!points":
      if (getPoints(param) > -1) {
        message.channel.send(param + " has " + points + " points");
      }
      break;
  }
});

function getPoints(username) {
  var GUILD = bot.guilds.values().next().value;

  var sql = "SELECT points, user_name FROM users WHERE user_name=" + "'" + username + "'";
  db.query(sql, function(err, result) {
    if (err) throw err;
    if (!resultEmpty(result)) { //The following console log outputs an actual numerical value for result[0].points (not undefined)
      console.log(result[0].user_name + " has " + result[0].points + " points");
      return result[0].points;
    } else {
      console.log("User with the name " + username.toString() + " not found")
      return -1;
    }
  });
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320

1 Answers1

3

Your getPoints function isn't returning anything at the moment, so it defaults to undefined. Have it return a Promise that resolves to the value of points instead so that you can call .then on it:

bot.on("message", function(message) {
  if (message.author.equals(bot.user)) return;

  var command = message.content.split(" ")[0];
  var param = message.content.split(" ")[1];

  if (!VALID_CMDS.includes(command)) return;
  var GUILD = bot.guilds.values().next().value;
  switch (command) {
    case "!hello":
      if (param) {
        message.channel.send("Hello, " + param);
      } else {
        message.channel.send("Use a second parameter and try again. FORMAT: !hello <name_goes_here>");
      }
      break;
    case "!points":
      getPoints(param)
        .then((points) => {
        if ( > -1) { //The return value of getPoints() is undefined
          message.channel.send(param + " has " + points + " points");
        }
      }
    break;
  }
});

function getPoints(username) {
  return new Promise((resolve, reject) => {
    var GUILD = bot.guilds.values().next().value;

    var sql = "SELECT points, user_name FROM users WHERE user_name=" + "'" + username + "'";
    db.query(sql, function(err, result) {
      if (err) return reject(err);
      if (!resultEmpty(result)) { //The following console log outputs an actual numerical value for result[0].points (not undefined)
        console.log(result[0].user_name + " has " + result[0].points + " points");
        resolve(result[0].points);
      } else {
        console.log("User with the name " + username.toString() + " not found")
        resolve(-1);
      }
    });
  });
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • This worked perfectly thank you! Can you explain why I had to use a promise to correct this? What were the issues with my old return statements? – Justin Hernandez Jun 10 '18 at 05:11
  • 1
    Your `getPoints` function wasn't returning anything before. Returning something to the `db.query` function as you were doing will just result in it being ignored. – CertainPerformance Jun 10 '18 at 05:17