I am trying to get MySQL to result from the function.
At moment I am trying to set results from function to global array, but this doesn't work.
I am not very familiar with NodeJS or Javascript but I think it's a scope issue.
How would one do this in a proper way? Do I need to use async or maybe return results from a function?
This is what I have at moment.
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'xxxx',
port : '3306',
user : 'xxxx',
password : 'xxxx',
database : 'xxxx'
});
var db_members =[];
get_members();
console.log(db_members); //outputs []
function get_members(){
connection.query("SELECT * FROM users", (err, result, fields)=>{
if (err) throw err;
result.forEach(function(row) {
db_members.push(row.username);
});
console.log(db_members); //this works
});
}
connection.query is async function so you are not able to get the result synchronously.
It is needed to make get_members to return Promise as the return value so to get the result asyncronously when using it..
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'xxxx',
port : '3306',
user : 'xxxx',
password : 'xxxx',
database : 'xxxx'
});
function get_members() {
return new Promise((resolve, reject) => {
connection.query("SELECT * FROM users", (err, result, fields)=>{
if (err) return reject(err);
var db_members = [];
result.forEach(function(row) {
db_members.push(row.username);
});
return resolve(db_members);
});
});
}
get_members()
.then((members) => {
console.log(members); // This will work.
})
.catch((error) => {
console.error(error);
});
Related
I want to store mysql result to a variable, so i can pass to the template engine. I have multiple mysql queries and need to save every query result to different variables.
I am using Node JS and express JS
Below is my code (app.js)
var mysql = require('mysql');
var db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'my_db'
});
db.connect(function(err) {
if (err) throw err;
console.log('Database connected');
});
app.get('/', (req, res) => {
var emp_result;
var task_list_result;
db.query('SELECT * FROM emp', function (error, results, fields) {
if (error) throw error;
emp_result = results;
});
db.query('SELECT * FROM task_list', function (error, results, fields) {
if (error) throw error;
task_list_result;
});
res.render('dashboard',{"emp_res" : emp_result, "task_res" : task_list_result});
});
I am expecting that result should be store in a variable;
Using async await here to handle the asynchronous network calls. I f you want to use callback function, you should chain it one inside the other.
const {promisify} = require('util');
app.get('/', async (req, res) => {
const query = promisify(db.query).bind(db);
const emp_result = await db.query('SELECT * FROM emp')
const task_list_result = await db.query('SELECT * FROM task_list')
res.render('dashboard',{"emp_res" : emp_result, "task_res" : task_list_result});
});
Using Promise.all to make calls in parallel, as they are not dependent on each other, it increases the performance, you can use the below code-
const {promisify} = require('util');
app.get('/', async (req, res) => {
const query = promisify(db.query).bind(db);
const [emp_result, task_list_result] = await Promise.all([db.query('SELECT * FROM emp'), db.query('SELECT * FROM task_list')]);
res.render('dashboard',{"emp_res" : emp_result, "task_res" : task_list_result});
});
Using callback function, as you were using -
app.get('/', (req, res) => {
db.query('SELECT * FROM emp', function (error, results, fields) {
if (error) throw error;
const emp_result = results;
db.query('SELECT * FROM task_list', function (error, results, fields) {
if (error) throw error;
const task_list_result = results;
res.render('dashboard',{"emp_res" : emp_result, "task_res" : task_list_result});
});
});
});
You can use any of these, but second one is the best approach to follow.
Currently I am playing around with the MySQL library in Node.js however I have a question about the correct/most efficient way to be using this library.
According to w3schools the correct way to make a single query is to use code like this
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
However, say I wanted to make multiple queries which would be executed by an event for example how would I handle this? Should I create an "initialise" function which is executed as soon as the program runs such as this?
var mysql = require('mysql');
var database;
//Initialise database
function setupDatabase() {
database = mysql.createConnection({
host: token.host,
user: token.user,
password: token.password,
database: token.database,
port: token.port
});
}
//Imagine this could be called at any time after execution
function event() {
if(database != null) {
database.connect(function(err) {
if (err) throw err;
database.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
}
}
And also do I have to connect to the database each time I make a query or can I add the "database.connect" call to my setupDatabase function such as this?
var mysql = require('mysql');
var database;
//Initialise database
function setupDatabase() {
database = mysql.createConnection({
host: token.host,
user: token.user,
password: token.password,
database: token.database,
port: token.port
});
if(database != null) {
database.connect(function(err) {
if (err) throw err;
});
}
}
//Imagine this could be called at any time after execution
function event() {
if(database != null) {
database.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
}
}
My main concern is that calling the con.connect function every single time I make a query would be slow and although these are asynchronous I want to be using the correct/most efficient way possible. Feel free to correct me on any mistakes with the last two code snippets I have only tested the first one so far.
You have to make database connection only once per application livetime (unless you have disconnects). Then you may have as much queries as you want.
Just put database connection routine somewhere in sepparate file and then require it in your applicatin initialisation step.
// mysql.js
const mysql = require('mysql');
module.exports = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
Or require it anywhere you need database connection - it will return connected database object without reruning that code again and again.
// inex.js
const databse = require('./mysql')
database.query("SELECT * FROM customers")
I created a file which include a function that holds a pool and handles the connection to the database like this
let _this = {};
let POOL = null;
function getPool() {
return new Promise((resolve, reject) => {
if(POOL != null) {
resolve(POOL);
} else {
//create connection pool
POOL = connectionPool;
resolve(POOL);
}
});
}
function closePool(){
// close pool here
}
_this.getPool = getPool;
_this.closePool = closePool;
module.exports = _this;
Now you can call getPool() and will recive a pool of connections where you can execute your queries with.
I'm trying to use connection from a connection.js file and use it in different file webFrontend.js using exports object. Now what I get on running server is:
{
"Result": "undefinedThis is result"
}
That means connection is not defined. Why is it happening? connection is working fine if getConnection is created in same (webFrontend.js) file, but the problem is when I use getConnection in same exports function in connection.js hence the connection not defined error:
Here are 2 necessary files (routes file has no problem) that explains what I'm doing:
connection.js
var mysql = require('mysql');
exports.connExport = function () {
var connectionPool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'rockcity_followme'
});
if(connectionPool) {
connectionPool.getConnection(function (err, connection) {
if (err) {
return err;
} else {
return connection;
}
});
}else{
var abc="return error";
return abc;
}
}
webFrontend.js
var connObj=require('../Routes/connection.js');
var connection=connObj.connExport();
exports.getIndivRecords= function(req, res, next){
res.send({
Result: connection+"This is result"
});
return next();
};
No need for the .js file extension, it's automagically added for you.
The code below uses standard error-first callbacks
webFrontend.js
var connection = require('../Routes/connection');
exports.getIndivRecords = function(req, res, next){
// connection takes a standard error-first callback
connection(function(err, conn){
if (err) {
// Handle the error returned
console.log(err);
}
// The database connection is available here as conn
console.log( "Connection:" + conn);
// presumably you want to do something here
// before sending the response
res.send({
Result: conn + "This is result"
});
});
return next();
};
connection.js
var mySQL = require('mysql');
var connectionPool = mySQL.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'rockcity_followme'
});
var getConnection = function (cb) {
connectionPool.getConnection(function (err, connection) {
// pass the error to the callback
if (err) {
return cb(err);
}
cb(null, connection);
});
};
module.exports = getConnection;
First of all #Dan Nagle was right no need of .js
Second You are getting the connection undefinded because still the method doesnt returned with result.
Use promise to call your Connection.js method
Your node is single threaded async execution,
He doest wait for the method to return a result
1) Problem with your javascript is that
var connection=connObj.connExport();
in Creation stage connection was defined by javascript as undefined and as
connObj.connExport(); as still not returned with answer
it executed this function in which connection was undefined
exports.getIndivRecords= function(req, res, next){
res.send({
Result: connection+"This is result"
});
Use promise read this first so you can understand something about promise and callback if you are unable to solve than comment i will play with it.But first you try.Thanku
Understanding promises in node.js
Ok Try This I have used promise here
var connObj = require('../Routes/connection');
connObj.connExport().then(
function (connection) {
exports.getIndivRecords = function (req, res, next) {
res.send({
Result: connection + "This is result"
});
return next();
};
}).catch(function (err) {
res.status(400).send(err);
return;
});
var mysql = require('mysql');
exports.connExport = function () {
return new Promise(function (fulfill, reject) {
var connectionPool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'rockcity_followme'
});
if (connectionPool) {
connectionPool.getConnection(function (err, connection) {
if (err) {
return reject(err);
} else {
return fulfill(connection);
}
});
} else {
var abc = "return error";
return reject(abc);
}
});
}
When I run the server there are no error. But when I visit the site it starts to give out an error TypeError: Converting circular structure to JSON
my database.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
module.exports = {
connection: connection
}
My products.js
var db = require('../database');
function getProducts(request) {
var product = db.connection.query('SELECT * from products', function(err) {
// connected! (unless `err` is set)
});
request.reply(product);
}
I just started with node.
update
db.connection.query('SELECT * from products', function(err, results) {
if (err) throw err;
console.log(err);
var products = results;
});
returning null on console.
update
var query = db.connection.query('SELECT * from products;', function(error, rows, fields) {
console.log(rows);
var products = rows;
});
it seems like adding ; to the end of the query did it.
Another thing is now products is not defined
function getProducts(request) {
if (request.query.name) {
request.reply(findProducts(request.query.name));
}
else {
request.reply(products);
}
}
As for the answer of the last question:
var products = query._results;
so I'm currently using the following code to execute queries;
var mysql = require('mysql');
var config = require('./config');
var pool = mysql.createPool(config.mysql);
function query(statement){
return new Promise(function(resolve, reject){
pool.getConnection(function(err, connection) {
if(err) reject(err);
connection.query(statement, function(err, row){
connection.release();
if(err){
reject(err);
}
resolve(row);
})
});
});
}
module.exports = {
pool: pool,
query: query
};
Whenever the query function is called it results in a undefined error;
TypeError: Cannot read property 'query' of undefined
I'm quite out of ideas why this could be, this would be connection.getConnection is not returning a proper connection, would this mean my credentials are wrong in my createPool function?
config.mysql
mysql: {
host: 'localhost',
user: 'root',
password: '',
database: 'site',
connectionLimit : 10,
multipleStatements : true
}
The problem is on the following line you are passing in the query function when you should be passing in a string.
connection.query(query, function(err, row){
query in that case should be a string. But you defined query as a function (function query(){)
If you change the line to something LIKE the following this should work.
connection.query("SELECT * from Users", function(err, row){