Proper asynchronous mysql query in nodejs? - mysql

Completely new to nodejs, having trouble wrapping my head around asynchronous programming/callbacks. What I'm trying to do is:
On 'post', I want to gather all the words in a table in my database. I call it like so: var lesson_data = init_load_lesson(); This call to init_load_lesson() is from index.js in my 'routers' file made my express.
How do I construct a proper callback so that lesson_data is the results of my query?
var mysql = require('mysql');
var connection = require('./connection');
var data = [];
function init_load_lesson()
{
connection.connect();
var queryString = "SHOW COLUMNS FROM LessonOneVocabulary";
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
else
{
for (var i in rows)
{
data.push(rows[i].Fields);
console.log(data);
}
console.log(data);
}
});
connection.end();
}
module.exports = function() {
load_lesson();
};

To add a callback (try a few more functions and you'll get the hang of it):-
function init_load_lesson(callback) {
... // connect to database
if (err) {
callback(err);
} else {
// process item
callback(null, data);
}
}
init_load_lesson(function(err2, results) {
if (err2) {
// process error
} else {
// process results
}
});

Related

Node js and mysql return query result

hi i'm newly learning nodejs and connected mysql database
now i want to save the result of a select query in a variable of some type but i cant.
var campGround = [];
console.log("Select Statement started....");
con.connect(function(error){
if(!error){
console.log("Connected");
var sql = "select * from campgrounds";
con.query(sql,function(err,result,field){
if (!err) {
// console.log(JSON.parse(result));
for(var i =0;i<result.length;i++)
{
try {
// console.log(result[i]);
setCampground(result[i]);
// campGround.push(result[i]);
} catch (error) {
console.log(error.message);
}
}
}
else{
console.log("Error while selecting record from campground table. ");
}
});
}else{
console.log("Error DataBase Not Connected!!! select statement");
}
});
function setCampground(value){
this.campGround.push(value);
}
console.log("length after execution :: "+campGround.length);
campGround.forEach(function(value){
console.log("Campground array");
console.log(value);
});
when i execute the above code and debug it...the select statement return from the database 3 records...but when i push them into the array ... and print the array nothing happens...please help
i cant find anything that could help me.
Your mysql query call is asynchronous (callback based) and your call to log the campGround is outside that callback so that means you are making a call but not waiting for that call to finish. That is the reason your campGround is not printing any thing.
You need to move following lines inside the callback where are are handling the error and response. something like this
const campGround = [];
console.log("Select Statement started....");
con.connect(function (error) {
if (!error) {
console.log("Connected");
const sql = "select * from campgrounds";
con.query(sql, function (err, result, field) {
if (!err) {
// console.log(JSON.parse(result));
for (let i = 0; i < result.length; i++) {
try {
// console.log(result[i]);
setCampground(result[i]);
// campGround.push(result[i]);
} catch (error) {
console.log(error.message);
}
}
} else {
console.log("Error while selecting record from campground table. ");
}
console.log(`length after execution :: ${campGround.length}`);
campGround.forEach(function (value) {
console.log("Campground array");
console.log(value);
});
});
} else {
console.log("Error DataBase Not Connected!!! select statement");
}
});
function setCampground(value) {
this.campGround.push(value);
}
You have:
const campGround = [];
that is global variable (or module scope);
Then in the code you have a function
function setCampground(value) {
this.campGround.push(value);
}
which is also in global scope (or module scope)
Therefore this.campGround is not campGround;
Change this.campGround .push(value); into campGround .push(value); and everything should work now.

nodejs- unable to return result to controller function

From my Model, I fetch some articles from a MySQL database for a user.
Model
var mysql = require('mysql');
var db = mysql.createPool({
host: 'localhost',
user: 'sampleUser',
password: '',
database: 'sampleDB'
});
fetchArticles: function (user, callback) {
var params = [user.userId];
var query = `SELECT * FROM articles WHERE userId = ? LOCK IN SHARE MODE`;
db.getConnection(function (err, connection) {
if (err) {
throw err;
}
connection.beginTransaction(function (err) {
if (err) {
throw err;
}
return connection.query(query, params, function (err, result) {
if (err) {
connection.rollback(function () {
throw err;
});
}
//console.log(result);
});
});
});
}
This is working and the function fetches the result needed. But it's not returning the result to the controller function (I am returning it but I'm not able to fetch it in the controller function. I guess, I did something wrong here).
When I did console.log(result) this is what I got.
[ RowDataPacket {
status: 'New',
article_code: 13362,
created_date: 2017-10-22T00:30:00.000Z,
type: 'ebook'} ]
My controller function looks like this:
var Articles = require('../models/Articles');
exports.getArticle = function (req, res) {
var articleId = req.body.articleId;
var article = {
userId: userId
};
Articles.fetchArticles(article, function (err, rows) {
if (err) {
res.json({ success: false, message: 'no data found' });
}
else {
res.json({ success: true, articles: rows });
}
});
};
Can anyone help me figure out what mistakes I made here?
I'm pretty new to nodejs. Thanks!
The simple answer is that you're not calling the callback function, anywhere.
Here's the adjusted code:
fetchArticles: function (user, callback) {
var params = [user.userId];
var query = `SELECT * FROM articles WHERE userId = ? LOCK IN SHARE MODE`;
db.getConnection(function (err, connection) {
if (err) {
// An error. Ensure `callback` gets called with the error argument.
return callback(err);
}
connection.beginTransaction(function (err) {
if (err) {
// An error. Ensure `callback` gets called with the error argument.
return callback(err);
}
return connection.query(query, params, function (err, result) {
if (err) {
// An error.
// Rollback
connection.rollback(function () {
// Once the rollback finished, ensure `callback` gets called
// with the error argument.
return callback(err);
});
} else {
// Query success. Call `callback` with results and `null` for error.
//console.log(result);
return callback(null, result);
}
});
});
});
}
There's no point in throwing errors inside the callbacks on the connection methods, since these functions are async.
Ensure you pass the error to the callback instead, and stop execution (using the return statement).
One more thing, without knowing the full requirements of this:
I'm not sure you need transactions for just fetching data from the database, without modifying it; so you can just do the query() and skip on using any beginTransaction(), rollback() and commit() calls.

ExpressJS - How to Properly Close MySQL Connection

I'm using ExpressJS importing MySQLJS. With ExpressJS as back-end, I have a service called /get-candidates where ExpressJS tries to fetch a couple of data from MySQL table and is returned to requester. I'm looking for a way to properly close MySQL DB Connection before returning the JSON to requester.
Here's what my /get-candidates looks like:
module.exports.getCandidates = function (request, response) {
var mysql = require("mysql");
var connectionSettings = require("../db.conf.json");
var connection = mysql.createConnection(connectionSettings);
connection.connect();
connection.query('SELECT * FROM Candidates', function (err, rows, fields) {
if (err) {
throw err;
} else {
response.json(rows);
}
});
connection.end(); // I don't think the code reaches this part after line 'response.json(rows)'
};
You can close connection once you get the query results either it is error or successfully fetched records.
module.exports.getCandidates = function(request, response) {
var mysql = require("mysql");
var connectionSettings = require("../db.conf.json");
var connection = mysql.createConnection(connectionSettings);
connection.connect();
connection.query('SELECT * FROM Candidates', function(err, rows, fields) {
connection.end();
if (err) {
throw err;
} else {
response.json(rows);
}
});
};
I don't see why you want to achieve this, but all you have to do is make a variable and send it a response after connection.
module.exports.getCandidates = function (request, response) {
var mysql = require("mysql");
var connectionSettings = require("../db.conf.json");
var connection = mysql.createConnection(connectionSettings);
var myRows; // our variable
connection.connect();
connection.query('SELECT * FROM Candidates', function (err, rows, fields) {
if (err) {
throw err;
} else {
myRows = rows;
//response.json(rows);
}
});
connection.end();
console.log(myRows); // To check if we have the value
response.json(myRows);
};

How to promisify a MySql function using bluebird?

Some time ago I decided to switch from PHP to node. In my first projects I didn't want to use any ORM since I thought that I didn't need to complicate my life so much learning another thing (at the moment I was learning node and angular) therefor I decided to use mysql package without anything else. It is important to say that I have some complex queries and I didn't want to learn from sctratch how to make them work using one of the 9000 ORM node have, This is what I've been doing so far:
thing.service.js
Thing.list = function (done) {
db.query("SELECT * FROM thing...",function (err,data) {
if (err) {
done(err)
} else {
done(null,data);
}
});
};
module.exports = Thing;
thing.controler.js
Thing = require('thing.service.js');
Thing.list(function (err,data) {
if (err) {
res.status(500).send('Error D:');
} else {
res.json(data);
}
});
how can I promisify this kind of functions using bluebird ? I've already tried but .... here I am asking for help. This is what I tried
var Thing = Promise.promisifyAll(require('./models/thing.service.js'));
Thing.list().then(function(){})
I have done this way and it is working fine.
const connection = mysql.createConnection({.....});
global.db = Bluebird.promisifyAll(connection);
db.queryAsync("SELECT * FROM users").then(function(rows){
console.log(rows);});
I have never had much luck with promisifyAll and IMO I prefer to handle my internal checks manually. Here is an example of how I would approach this:
//ThingModule
var Promises = require('bluebird');
Things.list = function(params) {
return new Promises(function(resolve, reject) {
db.query('SELECT * FROM thing...', function(err, data) {
return (err ? reject(err) : resolve(data));
});
});
}
//usage
var thinger = require('ThingModule');
thinger.list().then(function(data) {
//do something with data
})
.error(function(err) {
console.error(err);
})
You can also create a function that fires SQL like this :-
function sqlGun(query, obj, callback) {
mySQLconnection.query(query, obj, function(err, rows, fields) {
if (err) {
console.log('Error ==>', err);
// throw err;
return (err, null);
}
console.log(query)
if (rows.length) {
return callback(null, rows);
} else {
return callback(null, [])
}
});
}
Where mySQLconnection is the connection object you get after mysql.createConnection({}).
After that, you can promisify the function and use the promise like below :-
var promisified = Promise.promisify(sqlGun);
promisified(query, {}).then( function() {} );

Node.js, Express and Mysql. How is correct way

What i'am trying to achieve is to make DB query inside closure. Return data and then send stuff to user. I understand that best practice is to use database pooling. Problem is that query is not sync.
Simplified code:
server.js
var express = require('express'),
app = express(),
server = require('http').createServer(app),
mysql = require('mysql');
app.set('DB:pool', mysql.createPool(process.env.DATABASE_URL));
var myClosure = require('./closure.js')(app));
app.get('/somepage', function(req, res) {
var data = myClosure.myquery();
res.send(data);
});
app.get('/anotherpage', function(req, res) {
var data = myClosure.myquery();
res.send(data);
});
app.listen(3000);
closure.js
function myClosure(app) {
var pool = app.get('DB:pool');
return {
myquery: function(inp) {
pool.getConnection(function(err, db) {
if (err) throw err;
db.query('SELECT * FROM table', function(err, rows, fields) {
if (err) throw err;
data = rows[0]
db.release();
});
});
return data;
}
};
}
module.exports = myClosure;
In examples i found all DB related stuff were made in route callback and response was sent in query callback. But way i'm trying to do it is not working as myquery returns undefined because sql query is not done there.
So what is correct way to handle querys ?
Make your query-function handle a callback too:
// server.js
app.get('/somepage', function(req, res) {
myClosure.myquery(function(err, data) {
// TODO: handle error...
res.send(data);
});
});
// closure.js
...
myquery: function(callback) {
pool.getConnection(function(err, db) {
if (err) return callback(err);
db.query('SELECT * FROM table', function(err, rows, fields) {
// release connection before we return anything, otherwise it
// won't be put back into the pool...
db.release();
if (err) return callback(err);
callback(null, rows[0]);
});
});
}
(I left out the inp argument because that didn't seem to be used)