loopback custom query did not give response - mysql

my loopback custom query cannot give response after some time. below is my code.
var ds = tbljourney.dataSource;
ds.connector.query("select * from user where email='" + data.email + "'"
, function (err, user) {
user = user[0];
console.log("user", user);
if (err) {
console.log(err);
}
else if (user) {
}
});
some time it give a response but after 50 or 60 attemps its stuck

try to run your queries using the method below. Here's I'm connecting to an Oracle database. A lot of concat gets messy.
Model.remoteMethod(data,cb){
var app = require('../server');
var ds = app.datasources.newDatasource;
var sql = "SELECT * FROM TABLE WHERE SITE_ID=:param1 AND "+
"(DESTINATION=:param2 OR USERNAME=:param2 )";
var params = [param1, param2];
ds.connector.execute(sql, params, function(err, result){
if (err) console.error("error: " + err);
cb(err, result);
});
}

Related

NPM - MYSQL - Passing query results to another variable

I was looking for some help in regards to a database package that I'm building for a larger application. The application will essentially keep certain information on a database through the use of the mysql package in npm. The problem I'm facing is that when I try to pass the results variable to a variable that I've created I always get undefined. Though if I place a log statement underneath the variable transfer statement displaying the results variable it will be properly populated. I've done a fair amount of research and believe that this has something to do with promises and/or the callback function that's part of the query. I was wondering if I could have someone examine my code and let me know the best course of action. I've spent a couple of hours on this researching online and trying various solutions but nothing has worked.
const mysql = require("mysql");
var sqlResults;
var pool = mysql.createPool({
host: "example.com",
user: "exampleUser",
password: "123456Password",
database: "TestDB"
});
module.exports = {
...
databaseSelect: function(table, fields, conditionalStmt) {
pool.getConnection(function(err, connection) {
if (err)
throw err;
console.log("Connected to the example DB!");
var sql = "SELECT " + fields + " FROM " + table + " " + conditionalStmt;
connection.query(sql, function(error, results, fields) {
console.log("Successfully retrieved records from " + table + "\n\t" + sql);
sqlResults = results;
connection.release();
sqlResults = results;
console.log(results);
if (error)
throw error;
});
});
console.log(sqlResults);
return sqlResults;
}
}
Here's a sample of the output that I'm receiving:
I am ready!
undefined
undefined
Connected to the example DB!
Successfully retrieved records from User_Level_Info
SELECT HashID, Level, Experience FROM User_Level_Info WHERE HashID = 'e578059cabc6f937f0219127384126143e272acbac52c331345d573e0f085d21'
[ RowDataPacket {
HashID: 'e578059cabc6f937f0219127384126143e272acbac52c331345d573e0f085d21',
Level: 1,
Experience: 0 } ]
Here convert it into a Promise.
So this will wait until it gets resolved or reject
databaseSelect: function(table, fields, conditionalStmt) {
return new Promise(function(resolve, reject) {
pool.getConnection(function(err, connection) {
if (err)
return reject(err);
console.log("Connected to the example DB!");
var sql = "SELECT " + fields + " FROM " + table + " " + conditionalStmt;
connection.query(sql, function(error, results, fields) {
console.log("Successfully retrieved records from " + table + "\n\t" + sql);
sqlResults = results;
connection.release();
sqlResults = results;
console.log(results);
if (error)
return reject(error);
});
});
resolve(sqlResults);
});
}
//call your function
databaseSelect(params)
.then(function(rows) {
console.log(rows)
})
.catch((err) {
console.log(err)
}); // Throw async to escape the promise chain

How to send multiple responses in node js

How to send multiple response in node js? In this code I want to send data and data1
function(req, res){
var classValue = req.query.classID;
var sql = "SELECT * FROM class WHERE `class_name` ='"+classValue+"'";
connection.query(sql, function(err, data){
if(data.length){
var sql1 = "SELECT * FROM book WHERE `class_id` ='"+data[0].class_id+"'";
connection.query(sql1, function(err, data1){
res.status(200).send(data1);
res.status(200).send(data);
});
}
});
}
Calling send will end the connection, so you can only call it once. You can send back a complex structure as JSON easily like this:
res.json( { data, data1 } );
Note that you have a path where data.length might be zero that still needs to be handled. You might consider something like this:
function(req, res, next){
var classValue = req.query.classID;
var sql = "SELECT * FROM class WHERE `class_name` ='" + classValue + "'";
connection.query(sql, function(err, data) {
if (err) return next(err); // <-- handle errors
if (data.length) {
var sql1 = "SELECT * FROM book WHERE `class_id` ='" + data[0].class_id + "'";
connection.query(sql1, function(err, data1) {
if (err) return next(err); // <-- handle errors
res.json( { data, data1 } );
});
}
else {
res.json( {data: null, data1: null} ); // or treat it as an error?
}
});
}
If it's an error condition that the first query gives no results, then you can treat it as an error. Note that I also added error handling using the next function (assuming you're using Express routing).

problem while using for loop in async function in node js es6

I have written this piece of code that retrieves a list of contacts and in response show a JSON everything works fine but the code doesn't show any output what should I have done? this is my code:
export const ContactListPost = (req, res) => {
//const userHeader = req.get('userid');
const contacts = req.body;
console.log(contacts);
let users = [];
const delay = contact => new Promise(function (resolve, reject) {
let sql = "select user.userid, user.fullname, user.phone,
photos.name as photo_link " +
"from user left outer join photos on photos.uploader_id = user.userid where user.phone = ?";
console.log("contact " + contact);
con.query(sql, [contact], function (err, result) {
if (err){
console.log(err);
reject(err);
} else{
if (result.length > 0) {
users.push(result);
console.log(result);
resolve(result);
}else{
console.log("empty number");
//reject(result);
}
}
})
});
(async function loop() {
for (let i = 0; i< contacts.length ; i++){
let result = await delay(contacts[i].number);
users.push(result);
console.log("result " + JSON.stringify(result));
console.log("i " + i);
}
console.log("users " + JSON.stringify(users));
res.json(JSON.stringify(users));
})();
};
this piece of code doesn't show
console.log("users " + JSON.stringify(users));
res.json(JSON.stringify(users));
thanks for advice
If the promise is rejected, the await expression throws the rejected value.
You need to handle rejected promises. If one of the calls to delay returns a rejected promise, the rejected value is thrown and the execution of the function will stop and control will be passed to the first catch block in the call stack.
await operator
throw statement

Node js call function, that access mysql database and returns json result, multiple times

I'm new to Node.js. I have a function 'getFromDb' that accesses a mysql database and returns a json file with some data. What if I have an array of query data and I want to call the same function through a for loop to get a json file for each element of the array?
var http = require('http');
http.createServer(function(req, res) {
console.log('Receving request...');
var callback = function(err, result) {
res.setHeader('Content-disposition', 'attachment; filename=' + queryData+ '.json');
res.writeHead(200, {
'Content-Type' : 'x-application/json'
});
console.log('json:', result);
res.end(result);
};
getFromDb(callback, queryData);}
).listen(9999);
function getFromDb(callback, queryData){
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'xxxx',
password : 'xxxx',
database : 'xxxx',
port: 3306
});
connection.connect();
var json = '';
var data = queryData + '%';
var query = 'SELECT * FROM TABLE WHERE POSTCODE LIKE "' + data + '"';
connection.query(query, function(err, results, fields) {
if (err)
return callback(err, null);
console.log('The query-result is: ', results);
// wrap result-set as json
json = JSON.stringify(results);
/***************
* Correction 2: Nest the callback correctly!
***************/
connection.end();
console.log('JSON-result:', json);
callback(null, json);
});
}
You could use the async library for node for this. That library has many functions that make asynchronous programming in NodeJS much easier. The "each" or "eachSeries" functions would work. "each" would make all the calls to mysql at once time, while "eachSeries" would wait for the previous call to finish. You could use that inside your getFromDB method for your array.
See:
https://github.com/caolan/async#each
var http = require('http'),
async = require('async');
http.createServer(function(req, res) {
console.log('Receving request...');
var callback = function(err, result) {
res.setHeader('Content-disposition', 'attachment; filename=' + queryData+ '.json');
res.writeHead(200, {
'Content-Type' : 'x-application/json'
});
console.log('json:', result);
res.end(result);
};
getFromDb(callback, queryData);}
).listen(9999);
function getFromDb(callback, queryData){
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'xxxx',
password : 'xxxx',
database : 'xxxx',
port: 3306
});
connection.connect();
var arrayOfQueryData = ["query1", "query2", "query3", "query4", "query5"];
var jsonResults = [];
async.each(arrayOfQueryData, function (queryData, cb) {
var data = queryData + '%';
var query = 'SELECT * FROM TABLE WHERE POSTCODE LIKE "' + data + '"';
connection.query(query, function(err, results, fields) {
if (err)
return cb(err);
console.log('The query-result is: ', results);
// wrap result-set as json
var json = JSON.stringify(results);
console.log('JSON-result:', json);
jsonResults.push(json);
cb();
});
}, function (err) {
connection.end();
// callbacks from getFromDb
if (err) {
callback(err);
}
else {
callback(null,jsonResults);
}
});
}
use async module. it is the best one. If u dont want to add new module try following;
var count = 0;
array.forEach(function(element) { //array of the data that is to be used to call mysql
++count; //increase counter for each service call
async.db.call(element, callback); //the async task
}
var data = [];
function callback(err, resp) {
--count;//subtract for each completion
data.push(resp)
if(count == 0) { //return data when all is complete
return data;
}
}
I would recommend the async module though. it is very good practice and useful.

Node.js mysql transaction

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.....