Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js在连接到mysql数据库后崩溃_Mysql_Sql_Node.js - Fatal编程技术网

Node.js在连接到mysql数据库后崩溃

Node.js在连接到mysql数据库后崩溃,mysql,sql,node.js,Mysql,Sql,Node.js,基本上,node.js将完美地运行该功能,它将继续运行约10秒,然后它将崩溃。下面是完整的代码,因为我不知道是什么部分导致了实际的错误,但我希望有人会看到我把事情搞砸了 const Discord = require("discord.js"); const client = new Discord.Client(); const config = require("./config.json"); var fs = require('fs'); var mysql = require('mys

基本上,node.js将完美地运行该功能,它将继续运行约10秒,然后它将崩溃。下面是完整的代码,因为我不知道是什么部分导致了实际的错误,但我希望有人会看到我把事情搞砸了

const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
var fs = require('fs');
var mysql = require('mysql');
const path = require('path');

client.on("ready", () => {
  console.log('Running...');
});

var con = mysql.createConnection({
    host: "",
    user: "",
    password: "",
    database: "",
});

client.on("message", async message => {
    if (message.author.bot) return;
    if (!message.guild) {
        message.reply('Please do not DM the bot.');
        return;
    };
    if (message.content.toLowerCase() == "!script") {
        if (message.channel.name != "bot-chat") {
            message.author.send('Please only use bot commands in the #bot-chat channel.');
            return;
        };
        con.connect(function(err) {
            if (err) throw err;
            con.query("SELECT * FROM Users", function (err, result, fields){
                if (err) throw err;
                result.forEach(function(User){
                    var UserDiscord = User.DISCORD;
                    var UserKey = User.SKEY;
                    if (usr_dc == message.author.username+"#"+message.author.discriminator) {
                        message.author.sendCode("php",usr_key)
                        .then()
                        .catch(console.log);
                        if (message.guild.name == "Towlie Boi") {
                            message.author.sendCode("lua",`_G.key='KeyHere'
    loadstring(game:HttpGet("https://basehosting.xyz/sCrIpT1"))()`)
                        .then()
                        .catch(console.log);
                        message.channel.send('Script has been sent to your DMs, If you did not receive a DM then you have not allowed the bot to send you DMs');
                        .then()
                        .catch(console.log);
                        };
                    };
                });
            });
        });
    } else if (message.channel.name == 'whitelist') {
        if (message.content.substring(0,3).toLowerCase() == '!wl') {
            con.connect(function(err) {
                if (err) throw err;
                con.query("SELECT * FROM Keys", function(err, result, fields) {
                    if (err) throw err;
                    result.forEach(function(Key) {
                        var key = Key.Code;
                        if (message.content.substring(4) == key) {
                            var query = "DELETE FROM `Keys` WHERE `Keys`.`Code` = \'"+message.content.substring(4)+"\'";
                            con.query(query, function(err) {
                                if (err) throw err;
                                con.query("INSERT INTO `Users` (`DISCORD`, `SKEY`, `IP`) VALUES ('"+message.author.username+"#"+message.author.discriminator+"', '"+message.content.substring(4)+"', '');", function(err) {
                                    if (err) throw err;
                                    message.reply('Whitelisted Succesfully');
                                    return;
                                });
                            });
                        };
                    });
                });
            });
        };
    };
};

client.login(config.token);

我真的看不出有什么错误,因为我刚刚开始为node.js编写代码,对javascript不太了解,我想我可能必须在数据排队后关闭连接,但这似乎也不起作用。谢谢您的回答:)

从一个单独的文件连接数据库怎么样

module.exports = {
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'bookstore',
    charset  : 'utf8'
}
并传递控制权

var database = require('../models/database.js')();

从单独的文件连接数据库连接怎么样

module.exports = {
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'bookstore',
    charset  : 'utf8'
}
并传递控制权

var database = require('../models/database.js')();