discord.js 更好的 sqlite3 消息 returns 未定义

discord.js better-sqlite3 message returns as undefined

太棒了,我正在尝试建立一个经济系统,但是,每当我键入消息 "eco bal ${args}",其中 args 是 table 的名称时,我得到 "undefined" 结果而不是“$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"

这有什么区别:

 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 对象)。

缺少的方法("fetch" 如果你愿意的话)是 "You have undefined" 结果的近因。如果另一个数据元素将给出所需的结果,一定要试试!

这是 Statement 对象上的 API documentation

好的各位,我知道了。

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

如无不妥之处,欢迎评论。但是这里的代码有效