Jump to content

Recommended Posts

Posted (edited)

Hello. I'm trying to connect to mysql database and execute a query but it doesn't work. It doesn't even print "Test" in the console. Here's the code:
database.js

var mysql = require('mysql');

var mysqlConfig = mp.config.mysql;
var mysqlc = mysql.createConnection({
    host: mysqlConfig.host,
    user: mysqlConfig.user,
    password: mysqlConfig.password,
    database: mysqlConfig.database
});

module.exports.createAccount = function(name) {
    try {
        console.log(1);
        var query = "INSERT INTO `accounts` (`name`, `money`) VALUES ?";
        var values = [name, 0];
        console.log("Connecting to database..");
        mysqlc.connect(function(err) {
            console.log("Test");
            if (err) {
                console.log("MYSQL ERROR: " + err);
            } else {
                console.log("Database connection successful!");
                mysqlc.query(query, values, (err, result) => {
                    if (err) throw err;
                    console.log(result);
                });
            }
        });
    } catch (err) {
        console.log(err);
    };
};

events.js

var db = require('./database');

mp.events.add('playerJoin', (player) => {
    db.createAccount(player.name);
});

This is my console output:
 

[N] Starting network... - OK: (IPv4-only) at 127.0.0.1:22005
(node:13956) ExperimentalWarning: The ESM module loader is experimental.
Loading packages
        "test" loaded
[P] Plugin bridge.dll loaded!
Started HTTP server at 22006 port.
1
Connecting to database..

 

Edited by Farianit
Posted (edited)
8 hours ago, MrPancakers said:

Have you tried letting the database just connect when your server starts and not inside of a function when a player joins.

Thank you for trying to help me, but that didn't work :(
 

var mysql = require('mysql');

var mysqlConfig = mp.config.mysql;
var mysqlc = mysql.createConnection({
    host: mysqlConfig.host,
    user: mysqlConfig.user,
    password: mysqlConfig.password,
    database: mysqlConfig.database
});

console.log("Connecting to database..");
mysqlc.connect((err) => {
    if (err) throw err;
    console.log("Database connection successful!")
});

module.exports.createAccount = function(name) {
    try {
        console.log(1);
        var query = "INSERT INTO `accounts` (`name`, `money`) VALUES ?";
        var values = [name, 0];
        mysqlc.query(query, values, (err, result) => {
            if (err) throw err;
            console.log(result);
        });
    } catch (err) {
        console.log(err);
    };
};

Console output:
 

Loading packages
        "test" loaded
Connecting to database..
[P] Plugin bridge.dll loaded!
Started HTTP server at 22006 port.
1

 

I'm just curious why it doesn't show any errors? If there's something wrong with my mysql server, it should throw an error, am I right?

Edited by Farianit
Posted (edited)

Ok, the issue was with my mysql query. But why console.log() and throw err inside the callback don't print anything inside the server console? To find out this I moved all my code inside a blank node js project and then it printed the error. And console.log inside the callback in node js project works too!

Edited by Farianit
Posted

Lol, I fixed my query, now it works in node js project, but it still doesn't work in rage mp. I think I tried almost everything, including printing all the errors in file with "fs" module. But it doesn't even able to print to a file inside callback function. Any suggestions?

Posted (edited)

Don't use throw err, just use if(err) return console.log(err) and it'll print your errors to the console.

The script works fine for me in RAGE:MP when I did that.

var mysql = require('mysql');

var mysqlc = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'test'
});

console.log("Connecting to database..");
mysqlc.connect((err) => {
    if (err) return console.log(err);
    console.log("Database connection successful!")
});

module.exports.createAccount = function(name) {
    console.log(1);
    var query = "INSERT INTO `accountss` (`name`, `money`) VALUES ?";
    var values = [name, 0];
    mysqlc.query(query, values, (err, result) => {
        if (err) return console.log(err);
        console.log(result);
    });
};

module.exports.createAccount("test")

 

Edited by MrPancakers

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...