JS无法查询MySQL数据库.错误:connection.query不是函数 [英] JS Can't query MySQL database. ERROR: connection.query is not a function

查看:33
本文介绍了JS无法查询MySQL数据库.错误:connection.query不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图查询我的mySQL数据库,但不能,因为我收到 TypeError:connection.query不是函数错误.有谁知道为什么吗?我不知道为什么会这样.

Trying to query my mySQL database but I can't because i get a TypeError: connection.query is not a function error. Does anyone have any idea why? I don't have any clue as to why this is happening.

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 中导出连接.

Simply you don't export the connection in database.js.

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

Once created connection, assign it to a variable like const connection and type at bottom of the file module.exports = connection.

这篇关于JS无法查询MySQL数据库.错误:connection.query不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆