Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const sql = require('../../node_modules/mysql');
- module.exports =
- {
- connect_to_db: function (sql_query)
- {
- let con = sql.createConnection({
- host: "localhost",
- user: config.server_username,
- password: config.server_password,
- database: config.database_name
- });
- con.connect((err)=> {
- if (err){
- console.log("Problem connecting to the DB!");
- return;
- }
- console.log("Connected to the DB!");
- });
- con.query(sql_query, (err, result) => {
- if (err) throw err;
- console.log('Data received from the DB');
- console.log(result);
- return result;
- });
- con.end((err) => {});
- }
- };
- const connect_to_DB = require('DB_Connection');
- let sql_query = "SELECT * FROM table";
- database_results.push(connect_to_DB.connect_to_db(sql_query));
- console.log(database_results);
- [ undefined ]
- Connected to the DB!
- Data received from the DB
- [ RowDataPacket {
- mail_id: ,
- from: ,
- to: ',
- subject: ,
- message: ,
- date:,
- read_date: } ]
- Process finished with exit code 0
- const sql = require('../../node_modules/mysql');
- module.exports =
- {
- connect_to_db: function (sql_query)
- {
- return new Promise((resolve, reject) => {
- (async () => {
- let con = sql.createConnection({
- host: "localhost",
- user: config.server_username,
- password: config.server_password,
- database: config.database_name
- });
- con.connect((err)=> {
- if (err){
- console.log("Problem connecting to the DB!");
- return;
- }
- console.log("Connected to the DB!");
- });
- con.query(sql_query, (err, result) => {
- if (err) throw err;
- console.log('Data received from the DB');
- console.log(result);
- resolve();
- return result;
- });
- con.end((err) => {});
- })();
- });
- }
- };
- [ Promise { <pending> } ]
Add Comment
Please, Sign In to add comment