I want to export some functions from my database.js file and require them into the other file login.js, which is in the same folder. I tried also to declare the require globally and in a function, both ways did not work. I am using node.js/express.js.
DATABASE.JS
let mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
database: 'minigames',
user: 'root',
password: 'root'
});
function connected(){
connection.connect((err) => {
if(err) throw err;
console.log("Connected...");
})
}
function select(attribute){
return new Promise((resolve, reject) => {
let sql = `SELECT ${attribute} FROM player`;
connection.query(sql, (err, result, field) => {
if(err) reject(err);
resolve(Object.values(JSON.parse(JSON.stringify(result))));
})
})
}
async function query(attribute) {
return await select(attribute);
}
module.exports = {connection, connected, select, query};
LOGIN.JS
let userList = [];
function checkLogin(){
const database = require('./database');
let userListPromise = database.query('username, password');
userListPromise
.then()
.then((values) =>{
let i = 0;
while(values[i] != null){
userList.push(values[i]);
i++;
}
console.log("list filled");
console.log(userList);
})
}
checkLogin();