Guest User

Untitled

a guest
Nov 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. const sql = require('../../node_modules/mysql');
  2.  
  3. module.exports =
  4. {
  5. connect_to_db: function (sql_query)
  6. {
  7. let con = sql.createConnection({
  8. host: "localhost",
  9. user: config.server_username,
  10. password: config.server_password,
  11. database: config.database_name
  12. });
  13.  
  14. con.connect((err)=> {
  15. if (err){
  16. console.log("Problem connecting to the DB!");
  17. return;
  18. }
  19. console.log("Connected to the DB!");
  20. });
  21.  
  22. con.query(sql_query, (err, result) => {
  23. if (err) throw err;
  24. console.log('Data received from the DB');
  25. console.log(result);
  26. return result;
  27. });
  28.  
  29. con.end((err) => {});
  30.  
  31.  
  32. }
  33. };
  34.  
  35. const connect_to_DB = require('DB_Connection');
  36. let sql_query = "SELECT * FROM table";
  37. database_results.push(connect_to_DB.connect_to_db(sql_query));
  38.  
  39. console.log(database_results);
  40.  
  41. [ undefined ]
  42.  
  43. Connected to the DB!
  44. Data received from the DB
  45. [ RowDataPacket {
  46. mail_id: ,
  47. from: ,
  48. to: ',
  49. subject: ,
  50. message: ,
  51. date:,
  52. read_date: } ]
  53.  
  54. Process finished with exit code 0
  55.  
  56. const sql = require('../../node_modules/mysql');
  57.  
  58. module.exports =
  59. {
  60. connect_to_db: function (sql_query)
  61. {
  62. return new Promise((resolve, reject) => {
  63. (async () => {
  64. let con = sql.createConnection({
  65. host: "localhost",
  66. user: config.server_username,
  67. password: config.server_password,
  68. database: config.database_name
  69. });
  70.  
  71. con.connect((err)=> {
  72. if (err){
  73. console.log("Problem connecting to the DB!");
  74. return;
  75. }
  76. console.log("Connected to the DB!");
  77. });
  78.  
  79. con.query(sql_query, (err, result) => {
  80. if (err) throw err;
  81. console.log('Data received from the DB');
  82. console.log(result);
  83. resolve();
  84. return result;
  85. });
  86.  
  87. con.end((err) => {});
  88.  
  89. })();
  90. });
  91. }
  92. };
  93.  
  94. [ Promise { <pending> } ]
Add Comment
Please, Sign In to add comment