【问题标题】:discord.js better-sqlite3 message returns as undefineddiscord.js better-sqlite3 消息返回未定义
【发布时间】:2020-04-22 21:48:35
【问题描述】:

我正在尝试创建一个经济系统,但是,每当我输入消息“eco bal ${args}”(其中 args 是表的名称)时,结果却是“未定义” “$ 0”应该是我需要的余额。这是代码。

client.on("message", message => {
  if(message.author.bot) return;
  const args = message.content.slice(config.prefix.length).trim().split(' ');
  const command = args.shift().toLowerCase();

  if (command === "testbal") {

  const table = sql.prepare(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '${args}';`).get();
  if (!table['count(*)']) {
    // If the table isn't there, create it and setup the database correctly.
    sql.prepare(`CREATE TABLE IF NOT EXISTS ${args} (id TEXT PRIMARY KEY, user TEXT, guild TEXT, bal INTEGER);`).run();
    // Ensure that the "id" row is always unique and indexed.
    sql.prepare(`CREATE UNIQUE INDEX idx_${args}_id ON ${args} (id);`).run();
    sql.pragma("synchronous = 1");
    sql.pragma("journal_mode = wal");
  }

  // And then we have two prepared statements to get and set the score data.
  client.getScore = sql.prepare(`SELECT * FROM ${args} WHERE user = ? AND guild = ?`);
  client.setScore = sql.prepare(`INSERT OR REPLACE INTO ${args} (id, user, guild, bal) VALUES (@id, @user, @guild, @bal);`);

}

});

//Actual code of the thing

client.on("message", message => {
  const args = message.content.slice(config.prefix.length).trim().split(' ');
  const command = args.shift().toLowerCase();
      if (command == "bal") {
  if (message.author.bot) return;
  let score;
  if (message.guild) {
    score = client.getScore.get(message.author.id, message.content.args);
    if (!score) {
      score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, args, bal: 0}
    }
    score.bal++;
}
    if (message.content.indexOf(config.prefix) !==0) return;

    const data = sql.prepare(`SELECT bal FROM ${args}`);
      message.channel.send(`You have ${data.bal}`)
    }
});

当我运行命令“eco bal”时,我得到“You have undefined”

【问题讨论】:

  • data 是一个 Statement 对象,它永远不会被执行。
  • 那么我应该使用You have ${score.bal}并删除const data = sql.prepare(`SELECT bal FROM ${args}`); 吗?

标签: node.js sqlite discord.js better-sqlite3


【解决方案1】:

这有什么区别:

 const table = sql.prepare(`SELECT count(*) FROM sqlite_master   
 WHERE type='table' AND name = '${args}';`).get();

还有这个:

const data = sql.prepare(`SELECT bal FROM ${args}`); 

第一个 table 准备并执行 (get) sql。第二个,data 只准备 sql(即创建一个 Statement 对象)。

缺少的方法(“获取”,如果你愿意的话)是“你有未定义”结果的直接原因。如果另一个数据元素会给出所需的结果,请务必尝试一下!

这是Statement 对象上的API documentation。

【讨论】:

  • API 文档中并没有完全列出“fetch”方法,但是,我看了一下并做了这样的事情:`` const stmt = sql.prepare(SELECT bal FROM ${args} WHERE bal = ?);常量数据 = stmt.get(bal); message.channel.send(You have ${data}) } }); ``
【解决方案2】:

好的,我想通了。

client.on("message", message => {
  if(message.author.bot) return;
  const args = message.content.slice(config.prefix.length).trim().split(' ');
  const command = args.shift().toLowerCase();

  if (command === "testbal") {

  const table = sql.prepare(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '${args}';`).get();
  if (!table['count(*)']) {
    // If the table isn't there, create it and setup the database correctly.
    sql.prepare(`CREATE TABLE IF NOT EXISTS ${args} (bal TEXT);`).run();
    // Ensure that the "id" row is always unique and indexed.
    sql.prepare(`CREATE UNIQUE INDEX idx_${args}_id ON ${args} (bal);`).run();
    sql.pragma("synchronous = 1");
    sql.pragma("journal_mode = wal");
  }

  // And then we have two prepared statements to get and set the score data.
  client.getScore = sql.prepare(`SELECT * FROM ${args} WHERE bal = ?`);
  client.setScore = sql.prepare(`INSERT OR REPLACE INTO ${args} (bal) VALUES (@bal);`);
  sql.prepare(`INSERT OR REPLACE INTO ${args} (bal) VALUES (0);`).run();
}

});

//Actual command now

client.on("message", message => {
  const args = message.content.slice(config.prefix.length).trim().split(' ');
  const command = args.shift().toLowerCase();
    if (command === "bal"); {
        if (message.author.bot) return;
    const data = sql.prepare(`SELECT bal FROM ${args}`).get();
      message.channel.send(`You have ${data.bal}`)
  }
});

除非这里有什么问题,请随时发表评论。但是这里的代码有效

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 2020-06-27
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2018-10-23
    • 2021-05-26
    • 2021-02-27
    相关资源
    最近更新 更多