I have the following code. I am relative new to nodejs &js
I want to get values in 1. log but i get undefined.
Only 2. log is outputed to the log.
I read nodeJS return value from callback and
https://github.com/felixge/node-mysql but there is no example about return value.
I donot know how to use return statement with the given example in node-mysql page.
exports.location_internal = function (req, res) {
var r = getExternalLocation(2);
// 1. log
console.log(r);
res.send( r);
}
var getExternalLocation = function (id) {
pool.getConnection(function(err, connection){
if(err) throw err;
var response = {};
connection.query( "select * from external_geo_units where geo_unit_id = "+id, function(err, rows){
if(err) throw err;
response.data= rows;
// 2. log
console.log(response);
return response;
});
connection.release();
});
};
It's asynchronous, so you have to pass in a callback to get the value when it's ready. Example:
exports.location_internal = function(req, res, next) {
getExternalLocation(2, function(err, rows) {
if (err)
return next(err);
console.log(rows);
res.send(rows);
});
};
function getExternalLocation(id, cb) {
pool.getConnection(function(err, conn) {
if (err)
return cb(err);
conn.query("select * from external_geo_units where geo_unit_id = ?",
[id],
function(err, rows) {
conn.release();
cb(err, rows);
});
});
}
Related
I just started learning node.js...
Here is an example of my code. In this example everything works.
But, I have a question. How to make several SQL queries and send results to template?
At the moment I can only do this for one query...
Thanks.
//connection database
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'test'
});
connection.connect(function (err){
if (err) throw err;
console.log('Database connected . . . \n\n');
});
router.get('/', function(req, res, next) {
var sql = 'SELECT * FROM `test`';
connection.query(sql, function(err, rows, field){
if (err) throw err;
res.render('index', {
data: rows
})
});
});
Here is an answer following my comment since you mentioned you couldn't figure it out on your own.
First snippet uses promises, a quick helper function, but no external library. Second snippet uses the external async.js library and is a bit more callback-heavy. Both of them tackle the problem assuming we want the queries to be executed in parallel.
With promises
router.get('/', async function(req, res, next) {
var queries = ['SELECT * FROM `test`',
'SELECT * FROM `test2`',
'SELECT * FROM `test3`'];
var allResults = [];
/*transform our `query` array into an array of promises, then
await the parallel resolution of all the promises*/
var allQueryRows = await Promise.all(queries.map(query => promiseQuery(query)));
/*'allQueryRows' is an array of rows, so we push each of those
into our results*/
allQueryRows.forEach(function(rows){
allResults.push(...rows);
});
res.render('index', {
data: allResults
})
});
function promiseQuery(sqlQuery){
return new Promise((resolve, reject) => {
connection.query(sqlQuery, function(err, rows, field){
if(err)
return reject(err);
resolve(rows);
})
})
}
With callbacks and async.js
const async = require('async');
router.get('/', function(req, res, next) {
var queries = ['SELECT * FROM `test`',
'SELECT * FROM `test2`',
'SELECT * FROM `test3`'];
var allResults = [];
async.each(queries, function(sqlQuery, callback){
connection.query(sqlQuery, function(err, rows, field){
if(err)
throw err;
allResults.push(...rows);
callback();
});
}, function(){
res.render('index', {
data: allResults
});
});
});
I tried to get result using mysql database query from called function but do not wait for result in called function. Following is my code for users.js file. I got result in getBankDetail function but do not get result in users function.
var db = require("../db/mysqlconnection");
function users(app){
app.get("/users",async function(req, res, next){
let bankDetail = await getBankDetail();
console.log("bankDetail",bankDetail); //Here I do not got result
return res.send(bankDetail);
});
}
async function getBankDetail(){
db.getConnection(async function(err, connection) {
if (err) throw err; // not connected!
await connection.query('SELECT * FROM bank', function (error, results, fields) {
connection.release();
if (error) throw error;
console.log("bank result",results); //Here I got result
return results;
});
});
}
module.exports = users;
My Question is why do not wait for result in called function? I also used async/await functionality.
function getBankDetail(){
return new Promise((resolve, reject) => {
db.getConnection(function(err, connection) {
if (err) reject(err); // not connected!
connection.query('SELECT * FROM bank', function (error, results, fields) {
connection.release();
if (error) reject(err);
console.log("bank result",results); //Here I got result
resolve(results);
});
});
});
}
And then you can use let bankDetail = await getBankDetail();
If you want to use await on your db.getConnection and connection.query you will have to use mysql2/promises library or promisify those functions yourself
Here is the implementation when you use the promisified version of your database driver:
async function getBankDetail(){
const connection = await db.getConnection();
const data = await connection.query('SELECT * FROM bank');
connection.release();
console.log("bank result", data[0]); //Here I got result
return data[0];
}
I am trying to retrieve insertId of the SQL and send it back to the front end. But when I do the res.send(result.insertId), I am getting a 500 Error. How can I deliver the insertId of the sql to the front end successfully?
router.post('/list', function(req, res, next) {
var lastIncrement = 0;
pool.getConnection(function (error, connection) {
connection.query('INSERT INTO test_table SET Name = ?, Age = ?', [req.body.Name, req.body.Age], function (error, result) {
if(error)
console.error(error);
lastIncrement = result.insertId;
connection.release();
});
});
res.send(lastIncrement);
});
There is one problem in your code for sure that you are returning before finishing the query. So you have to return from inside the callback function.
router.post('/list', function(req, res, next) {
var lastIncrement = 0;
pool.getConnection(function (error, connection) {
connection.query('INSERT INTO test_table SET Name = ?, Age = ?', [req.body.Name, req.body.Age], function (error, result) {
if(error)
console.error(error);
lastIncrement = result.insertId;
connection.release();
res.send(lastIncrement);
});
});
});
Due to asynchronous nature of nodejs your res.send is executing before the query returns the value. You can use a .then promise to ensure that the res.send happens after you get the value from query or have res.send after assigning the value.
You can try this:
pool.getConnection(function (error, connection) {
connection.query('INSERT INTO test_table SET Name = ?, Age = ?', [req.body.Name, req.body.Age], function (error, result) {
if(error)
console.error(error);
lastIncrement = result.insertId;
connection.release();
}).then(function(){
res.send(lastIncrement);
})
});
I tried to insert a encrypt value to db, i can encrypt the value the encrypted value can't be inserted in db.
app.post('/insert', function (req, res) {
// var Fname=req.body.fname;
// var Lname=req.body.pwd;
var data = {
Fname: req.body.fname,
Lname: req.body.Lname
};
function hashP(getit, cb) {
bcrypt.genSalt(15, function (err, salt) {
if (err) {
return console.log(err);
}
cb(salt);
bcrypt.hash(getit, salt, function (err, gotit) {
if (err) throw err;
return this.cb(null, gotit);
})
})
}
hashP(data.Lname, function (err, gotit) {
if (err) throw err;
data.Lname = hash;
})
console.log(data.Lname);
con.query("insert into test set ?", [data], function (err, rows) {
if (err) throw err;
res.send("Value has bee inserted");
})
})
This is my html form page:
<body>
<form action="http://localhost:8888/insert" method="POST" >
<label>Name:</label><input type="text" name="fname"></br>
<label>Lname:</label><input type="text" name="Lname"></br>
<button type="submit">Submit</button>
</form>
</body>
Seems like your function hashP(getit,cb) is calling cb function at bad time isn't it? try the following
function hashP(getit, cb){
bcrypt.genSalt(15, function (err, salt){
if(err) {
return cb(err, null);
}
bcrypt.hash(getit, salt, function (err, hash){
if(err) {
return cb(err, null);
}
return cb(null, hash);
})
})
}
Apart, you'll need to call it inside your handler as following:
app.post(...., function(req, res) {
var data = { ... }
function hashP(data, cb){ ... }
hashP(data.Lname, function (err, hash) {
if (err) throw err;
data.Lname = hash;
// NOW, SAVE THE VALUE AT DB
con.query("insert into test set ?", [data], function (err, rows) {
if (err) throw err;
res.send("Value has bee inserted");
})
}
}
The problem here was asynchronous execution, you were calling con.query with data before data is returned from hashP
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)