I have a method getconnection() that creates connection for me.
Here's the function.
I get undefined even if I am fetching result of query that is rows to callback function to send the response to controller.
Can someone help me out.Thanks in advance.
function fetchUserInfo(req,res)
{
var getUserInfo = "select First_Name, Last_Name,COUNT(*) FROM twitter.user_information INNER join twitter.tweets ON user_information.Twitter_Handle = tweets.Twitter_handle WHERE user_information.Twitter_Handle = '"+req.session.handle+"' ";
console.log("Fetch User-------------"+getUserInfo);
mysql.fetchUser(function(err,result){
if(err)
{ console.log("cannot be retrieved");
throw err;
}
else
{
console.log("Successfully retrieved");
console.log("---------------" + result);
console.log(result.First_Name);
res.send(result);
}
},getUserInfo);
}
// mysql function
function fetchUser(callback,sqlQuery)
{
console.log("In fetch Data");
console.log("SQLquery is:::"+sqlQuery);
connection.getConnection().query(sqlQuery, function(err,rows,fields){
if(err)
{
console.log("Error :"+err.message);
}
else
{
console.log("Result is :" + rows.First_Name);
callback(err, rows);
}
})
}
Try this:
console.log("Result is :" + rows[0].First_Name);
or try to print the rows variable to the console instead of rows.First_Name:
console.log(rows);
Related
Here's how I write my code
model\user.js
'user strict';
var sql = require('./../db.js');
//User object constructor
var User = function(user){
this.email = user.email;
this.name = user.name;
this.gender = user.gender;
this.profileImageType = user.profileImageType;
this.profileImage = user.profileImage;
this.accountType = user.accountType;
this.createdAt = new Date();
};
User.getCountByEmail = function (email, result) {
console.log("entering user.getCountByEmail with")
console.log(email)
sql.query("Select COUNT(*) AS userCount from users where email = ? ", email, function (err, res) {
if(err) {
console.log("error: ", err);
return result(err, null);
}
else{
console.log("user getCountByEmail returns")
console.log(res)
return result(null, res);
}
});
};
model\userController.js
'use strict';
var User = require('./user.js');
exports.list_all_users = function(req,res){
User.getAll(function(err,user){
console.log('controller')
if (err){
res.send(err);
}else{
console.log('res', user)
res.send(user)
}
});
};
exports.getCountByEmail = function(email,res){
User.getCountByEmail(email, function(err,user){
if (err){
console.log('error', err)
return res.send(err)
}else{
console.log('res', user)
console.log(user)
return user;
}
});
};
index.js
var userController = require('./../model/userController.js');
userController.getCountByEmail("miow#email.com",res,function(err,result) {
if (err){
console.log("34 err")
console.log(err)
}else{
console.log("imin userCount: " + result)
}
});
now in my index.js above, the line userController.getCountByEmail is executed, but I want the result to be accessible inside the function. Unfortunately, it seems the code
function(err,result) {
if (err){
console.log("34 err")
console.log(err)
}else{
console.log("imin userCount: " + result)
}
isn't being executed at all. I only got the line
res [ RowDataPacket { userCount: 1 } ]
imin
[ RowDataPacket { userCount: 1 } ]
which is inside userController.js. So how do I get the result from userController.js sent to index.js ?
function(err,result) {
if (err){
console.log("34 err")
console.log(err)
}else{
console.log("imin userCount: " + result)
}
is not executed because in your model/userController getCountByEmail your only pass two parameters (email, res). So when you call this function in your index.js your actually pass three parameters. with the third one being
function(err,result) {
if (err){
console.log("34 err")
console.log(err)
}else{
console.log("imin userCount: " + result)
}
I would do this:
model/userController
exports.getCountByEmail = function(email,res){
User.getCountByEmail(email, function(err,user){
if (err){
console.log('error', err)
return res.send(err)
}else{
console.log('res', user)
console.log(user)
res.send(user);
}
});
};
index.js
userController.getCountByEmail("miow#email.com",res)
But in general the controller take (req, res, next) as parameter and the res is used like a return because it returns a response to the server.
I'm trying to loop with a result from a mysql query and execute a new query in that loop. After looping thru the results I want to open a new view with the result of the first and second query.
Now my problem: The looping al works and I can open the new view, but when opening the view I only get the result of the first query and part of the second query, it opens the view before running the second query another time.
My code:
function getActivitiesDate(req,res, rows)
{
var rows2 = [];
rows.forEach(function getoutput(item) {
console.log("getActivitiesDate openend");
connectionpool.getConnection(function(err, connection3) {
console.log('Trying to connect to activities date');
if (!err) {
console.log('Trying to execute query for activities now'),
connectionpool.query('SELECT * FROM CALENDAR_ACTIVITY WHERE ACTIVITY_ID =' + connection3.escape(item.ACTIVITY_ID),
function (err, rows2) {
if (err) {
console.error(err);
res.statusCode = 500;
res.send({
result: 'error',
err: err.code
});
connection3.release();
console.log('It doesnt work error 500');
}
if (rows2.length == 0) {
res.render('index', {title: 'Index - Calendar Activity not found'});
}
console.log('Show activities + dates');
// connection3.release();
});
} else {
console.error('CONNECTION error: ', err);
res.statusCode = 503;
res.send({
result: 'error',
err: err.code
});
console.log('It does not work...');
}
});
});
console.log('This is the result of rows2Result: ');
console.log(rows2);
res.render('viewUserActivities', {activities:rows, activityDates:rows2 , title: 'Created activities'});
}
the "rows" attribute contains 2 rows for the moment (this could be more, or less, that's why I have the "rows.forEach") but I'm not sure how to pass the result of "rows2" to the new view AFTER it's looped thru the foreach loop for every result.
Any help would be greatly appreciated. I've it's not clear, or you need more information don't hesitate to ask.
I do not have sufficient rep to comment so I might be misinterpreting the question, but can you render the view after the forEach callback has finished?
Like so:
function getActivitiesDate(req,res, rows)
{
var rows2 = [];
rows.forEach(function getoutput(item) {
console.log("getActivitiesDate openend");
connectionpool.getConnection(function(err, connection3) {
console.log('Trying to connect to activities date');
if (!err) {
console.log('Trying to execute query for activities now'),
connectionpool.query('SELECT * FROM CALENDAR_ACTIVITY WHERE ACTIVITY_ID =' + connection3.escape(item.ACTIVITY_ID),
function (err, rows2) {
if (err) {
console.error(err);
res.statusCode = 500;
res.send({
result: 'error',
err: err.code
});
connection3.release();
console.log('It doesnt work error 500');
}
else if (rows2.length == 0) {
res.render('index', {title: 'Index - Calendar Activity not found'});
} else {
console.log('This is the result of rows2Result: ');
console.log(rows2);
res.render('viewUserActivities', {activities:rows, activityDates:rows2 , title: 'Created activities'});
console.log('Show activities + dates');
}
// connection3.release();
});
} else {
console.error('CONNECTION error: ', err);
res.statusCode = 503;
res.send({
result: 'error',
err: err.code
});
console.log('It does not work...');
}
});
I have an express.js based rest application. Please have a look on following code and suggest me what would be better way.
I want to select user and its associated images (1 user has many images).
function getUser (connection, req, res) {
var userId = req.params.id;
connection.query('SELECT * FROM user p'
+ ' WHERE p.id = ' + connection.escape(userId), function handleSql(err, rows) {
if (err){ logAndRespond(err,res); return; }
if (rows.length === 0){ res.send(204); return; }
var adId = rows[0].adId;
// load images
connection.query('SELECT id, url FROM image WHERE ad_id = ' + connection.escape(adId), function (err, imgRows) {
if (err){ logAndRespond(err,res); return; }
if (rows.length != 0){
rows[0].images = imgRows;
}
res.json({'user': rows});
connection.release();
});
});
}
You don't have to escape parameters by yourself
You don't release the connection if an error occurred
The problem now is I don't know what you want to do with selected rows. You are also checking the rows.length twice but if there weren't any records in the first query then the second one will not be executed.
function getUser(conn, req, res) {
conn.query("SELECT * FROM user p WHERE p.id = ?;", [req.params.id], function(err, rows) {
if (err) {
return logAndRespond(err, res);
}
if (!rows.length) {
return res.send(204);
}
conn.query("SELECT id, url FROM image WHERE ad_id = ?;", [rows[0].adId], function(err, imgRows) {
if (err) {
return logAndRespond(err, res);
}
if (rows.length) { // ???
rows[0].images = imgRows;
}
res.json({"user": rows});
conn.release();
});
});
}
Can anyone provide an example of how I could achieve MySQL transactions in Node.js. I am trying to get my head around using the node-mysql driver and node-mysql-queue.
As far are I can tell, using node-mysql-queue greatly reduces the asynchronous nature of Node.js as new queries have to wait until existing ones have completed. To get around this, has anyone attempted to combine node-mysql-queue with node-mysql's connection-pooling capabilities. i.e starting a new mysql connection for each new http request, and starting transaction queues on individual connections?
Update
See the edit below for async/await syntax
I spent some time writing a generalized version of the transaction example given by node mysql, so I thought I would share it here. I am using Bluebird as my promise library, and used it to 'promisify' the connection object which simplified the asynchronous logic a lot.
const Promise = ('bluebird');
const mysql = ('mysql');
/**
* Run multiple queries on the database using a transaction. A list of SQL queries
* should be provided, along with a list of values to inject into the queries.
* #param {array} queries An array of mysql queries. These can contain `?`s
* which will be replaced with values in `queryValues`.
* #param {array} queryValues An array of arrays that is the same length as `queries`.
* Each array in `queryValues` should contain values to
* replace the `?`s in the corresponding query in `queries`.
* If a query has no `?`s, an empty array should be provided.
* #return {Promise} A Promise that is fulfilled with an array of the
* results of the passed in queries. The results in the
* returned array are at respective positions to the
* provided queries.
*/
function transaction(queries, queryValues) {
if (queries.length !== queryValues.length) {
return Promise.reject(
'Number of provided queries did not match the number of provided query values arrays'
)
}
const connection = mysql.createConnection(databaseConfigs);
Promise.promisifyAll(connection);
return connection.connectAsync()
.then(connection.beginTransactionAsync())
.then(() => {
const queryPromises = [];
queries.forEach((query, index) => {
queryPromises.push(connection.queryAsync(query, queryValues[index]));
});
return Promise.all(queryPromises);
})
.then(results => {
return connection.commitAsync()
.then(connection.endAsync())
.then(() => {
return results;
});
})
.catch(err => {
return connection.rollbackAsync()
.then(connection.endAsync())
.then(() => {
return Promise.reject(err);
});
});
}
If you wanted to use pooling as you suggested in the question, you could easily switch the createConnection line with myPool.getConnection(...), and switch the connection.end lines with connection.release().
Edit
I made another iteration of the code using the mysql2 library (same api as mysql but with promise support) and the new async/await operators. Here is that
const mysql = require('mysql2/promise')
/** See documentation from original answer */
async function transaction(queries, queryValues) {
if (queries.length !== queryValues.length) {
return Promise.reject(
'Number of provided queries did not match the number of provided query values arrays'
)
}
const connection = await mysql.createConnection(databaseConfigs)
try {
await connection.beginTransaction()
const queryPromises = []
queries.forEach((query, index) => {
queryPromises.push(connection.query(query, queryValues[index]))
})
const results = await Promise.all(queryPromises)
await connection.commit()
await connection.end()
return results
} catch (err) {
await connection.rollback()
await connection.end()
return Promise.reject(err)
}
}
The following transaction example was added to the documentation a month ago:
https://github.com/felixge/node-mysql#transactions
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO posts SET title=?', title, function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
var log = 'Post ' + result.insertId + ' added';
connection.query('INSERT INTO log SET data=?', log, function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('success!');
});
});
});
});
I am using the following approach. There is an add function in my Model where I am performing database operations.
add : function (data, callback) {
//Begin transaction
connection.beginTransaction(function(err) {
if (err) {
throw err;
}
var user_query = "INSERT INTO `calldata`.`users` (`username`, `password`, `enabled`, `accountNonExpired`, `accountNonLocked`, `credentialsNonExpired`) VALUES ('" + data.mobile + "', '" + sha1(data.password) + "', '1', '1', '1', '1')";
connection.query(user_query, function(err, results) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
var accnt_dtls_query = "INSERT INTO `calldata`.`accnt_dtls` (`req_mob_nmbr`, `usr_nme`, `dvce_id`, `mngr_id`, `cmpny_id`, `actve_flg`, `crtd_on`, `usr`) VALUES (" + data.mobile + ", '" + data.name + "', '', " + data.managerId + ", " + data.companyId + ", 1, now(), '" + data.mobile+ "')";
connection.query(accnt_dtls_query, function(err, results) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
var user_role_query = "INSERT INTO `calldata`.`user_roles` (`username`, `ROLE`) VALUES ('" + data.mobile + "', '" + data.role + "')";
connection.query(user_role_query, function(err, result) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
//add an entry to manager table
var mngr_dtls_query = "INSERT INTO `calldata`.`mngr_dtls` (`mngr_nm`, `cmpny_id`, `crtd_on`, `usr_nm`, `eml_id`) VALUES ('" + data.name + "'," + data.companyId + " , now(), '" + data.mobile + "', '" + data.mobile + "')";
connection.query(mngr_dtls_query, function(err, result) {
if (err) {
return connection.rollback(function () {
throw err;
});
}
console.log('Changed ' + result.changedRows + ' results');
connection.commit(function (err) {
console.log('Commiting transaction.....');
if (err) {
return connection.rollback(function () {
throw err;
});
}
console.log('Transaction Complete.');
connection.end();
callback(null, result);
});
});
});
});
});
});
//transaction ends here
}
and calling from controller:
agentAccountModel.add(data, function(err, results) {
if(err)
{
res.status(500);
res.json({
"status": 500,
"message": err
});
}
res.status(200);
res.json({
"status": 200,
"message": "Saved successfully"
});
});
I have come up with a solution using a recursive function.
var sql = 'INSERT INTO logs SET data = ?';
// array of rows to insert
var rows = [[/*first row*/], [/*additional row*/]];
connection.beginTransaction(function (err) {
if (err) {
throw err;
}
var insertEachRow = function () {
var row = rows.shift();
if (! row) {
// Done, now commit
return noMoreRows();
}
connection.query(sql, row, function (err, result) {
if (err) {
connection.rollback(function () {
throw err;
});
}
insertEachRow();
});
};
var noMoreRows = function () {
connection.commit(function (err) {
if (err) {
connection.rollback(function () {
throw err;
});
}
console.log('success!');
});
};
insertEachRow();
});
I found one useful links which uses node js mysql pooling with transaction. Database Connection pooling is always useful. One can check this link
https://github.com/mysqljs/mysql
I created a wrapper ORM type thing for this specific purpose, hope it helps SQl-connecton - pool ORM type helper methods
You can use it with the mysql also, just use this function without any parameters /call back .
Make sure you have async function and use these instead
await con.rollback();
await con.beginTransaction();
await con.commit();
Life set.....
first, i connect the db and select DB:
var defaultOptions = {
user: "root",
pwd:'admin',
db:"britcham_dev_local",
server:"local", // Maybe we don't need this variable.
};
var client = new Client();
client.user = defaultOptions.user;
client.password = defaultOptions.pwd;
client.connect(function (error, results) {
//
});
client.query('USE ' + defaultOptions.db, function (error, results) {
//
});
Second, I query with client object:
var self = this;
var this.users;
client.query("SELECT * FROM users", function (error, results, fields) {
if (error) {
//
}
if (results.length > 0) {
self.users = results;
}
});
console.log(this.users);
it's nothing output ??? Why ??
Since node.js is non-blocking and asynchronous, then in this code:
client.query("SELECT * FROM users", function (error, results, fields) {
if (error) {
//
}
if (results.length > 0) {
self.users = results;
}
});
console.log(this.users);
data from DB are not probably loaded yet into users variable when you are trying to log it into console. You can check it out if you do your console.log operation within the query, for example:
client.query("SELECT * FROM users", function (error, results, fields) {
if (error) {
//
}
if (results.length > 0) {
console.log(results);
}
});
To pass the result into a variable when the operation is finished you can wrap your client DB call into a function with callback parameter and set your variable when the callback is invoked, for example:
function query(sql, callback) {
client.query(sql, function (error, results, fields) {
if (error) {
//
}
if (results.length > 0) {
callback(results);
}
});
}
query("SELECT * FROM users", function(results) {
self.users = results;
console.log(self.users);
});
Above code is just a concept.
How is the suggested answer different from this?
var self = this;
var this.users;
client.query("SELECT * FROM users", function (error, results, fields) {
if (error) {
//
}
if (results.length > 0) {
self.users = results;
console.log(this.users);
}
});
I might be wrong this is not different from the suggested answer in that it writes to console no sooner than when we have the data back from the DB.
The suggested answer seems only to add yet another function?