node.js-函数中的值返回未定义

tcbh2hod  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(357)

这个问题在这里已经有答案了

如何从异步调用返回响应(42个答案)
两年前关门了。
我正在使用discord.js开发一个机器人。我的目标是利用sql来存储用户信息及其要点。当我尝试使用!点命令,我从控制台收到正确的响应。我的天哪!然而,points case语句返回一个未定义的值。在没有if语句的情况下进行测试时,我得到这样一条消息:hasindefinedpoints。

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

axzmvihb1#

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

相关问题