Node.js - 函数中的值返回undefined

时间:2018-06-10 04:55:56

标签: javascript mysql node.js undefined return-value

我正在使用discord.js开发机器人。我的目标要求我利用SQL来存储用户信息及其要点。当我尝试使用!points命令时,我从控制台收到正确的响应。然而,我的!点案例陈述却返回了一个未定义的值。在没有if语句的情况下测试时,我得到一条消息,如下所示:有未定义的点。

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

1 个答案:

答案 0 :(得分:1)

您的getPoints函数此刻未返回任何内容,因此默认为undefined。让它返回Promise代替points的值,以便您可以在其上调用.then

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