首页 > 解决方案 > discord.js better-sqlite3 消息返回未定义

问题描述

Soooo 我正在尝试创建一个经济系统,但是,每当我输入消息“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”时,我得到“你有未定义”

标签: node.jssqlitediscord.jsbetter-sqlite3

解决方案


这有什么区别:

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

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

这是该对象的API 文档。Statement


推荐阅读