【问题标题】:JS Can't query MySQL database. ERROR: connection.query is not a functionJS 无法查询 MySQL 数据库。错误:connection.query 不是函数
【发布时间】:2020-12-21 01:49:14
【问题描述】:

尝试查询我的 mySQL 数据库,但我不能,因为我收到 TypeError: connection.query is not a function 错误。有谁知道为什么?我不知道为什么会这样。

database.js

const fs = require('fs'); // node.js file system module
require('dotenv').config(); // stores tokens, keys, passwords and other info
const Discord = require('discord.js'); // links discord.js api to file
const database = require('./database.js');

const client = new Discord.Client(); // creates bot user

let connection;
(async () => {
    connection = await require('./database.js');
    await client.login(process.env.TOKEN); // bot goes from offline to online
})();

client.once('ready', () => console.info(`[${date.toLocaleString()}] INFO | Ready, logged in as ${client.user.tag} (${client.user.id})\n------------------------`));

client.on('guildCreate', async guild => {
    try {
        await connection.query(`INSERT INTO guildInfo VALUES('${guild.id}', '${guild.ownerID}')`);
        await connection.query(`INSERT INTO guildConfig (guildID) VALUES('${guild.id}')`);
    } catch(err) {
        console.error(err);
    }
});

database.js

require('dotenv').config();
const mysql = require('mysql2/promise');

date = new Date();
mysql.createConnection({
    user: process.env.USER,
    password: process.env.PASSWORD,
    database: process.env.DATABASE
}).then(connection => console.info(`[${date.toLocaleString()}] INFO | Waiting for input/changes to code\n------------------------`)).catch(err => console.error(err));

错误

TypeError: connection.query is not a function
    at Client.<anonymous> (C:\Users\Patrick Lawrence\Desktop\Synth\index.js:43:20)
    at Client.emit (events.js:315:20)
    at Object.module.exports [as GUILD_CREATE] (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\handlers\GUILD_CREATE.js:33:14)
    at WebSocketManager.handlePacket (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\ws\lib\websocket.js:797:20)
    at Receiver.emit (events.js:315:20)

【问题讨论】:

  • database.js 似乎没有导出任何内容。
  • @aioros 谢谢!我将如何导出它?

标签: javascript mysql node.js discord.js


【解决方案1】:

只是你不导出database.js中的连接。

创建连接后,将其分配给const connection 之类的变量,然后在文件底部键入module.exports = connection

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2017-09-19
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多