Node.js Checkbox Iteration - mysql

How do I iterate through the checked checkboxes, without hard coding and checking each box one at a time. Kinda like this link, but with Node.js and not JavaScript
Iterate all checkbox in JavaScript:
So I'm attempting to display the contents of my database (using MySQL), but I don't want to hard code 'req.body.checkboxName' for every checkbox in case a new column is added onto a table (the checkboxes are each of the columns in a given table). So I need to find a way that will iterate through every checkbox in the webpage and see if it is checked. If it is then we'll add them together in a string and query for those columns. My code below is for the Node.js POST method (after hitting submit once the checkboxes are checked) and the other is my Pug/Jade code (the JavaScript Templating Engine that I use, sorry it isn't plain html).
In case you're confused how the Pug/Jade file runs initially, another GET method renders that Pug/Jade file with the column names; that GET method is basically the POST method below up until the '//Need help' line
Node.js
app.post('/GetTables', function(req, res){
var columnsRequest = 'DESCRIBE ' + tableName;
var columnsList = [];
connection.query(columnsRequest, function(err, results, fields) {
if(err){
throw err;
}
for (var index in results) {
console.log(results[index].Field);
columnsList.push(results[index].Field);
}
});
//Need help, the next 5 or so lines are non-working ~psuedocode
var checkedList = '';
req.body.CHECKBOXES.each(function(index, element){
if(CHECKBOX.ischecked(){
checkedList += CHECKBOX.name() + ', '
}
});
var mysqlRequest = 'SELECT ' + checkedList + ' FROM ' + tableName;
connection.query(mysqlRequest, function(err, results, fields) {
if(err){
throw err;
}
res.render('webPage', {'columnstodisplay': results});
});
});
Pug/Jade
form(method = 'POST', action = '/GetTables', id = 'tableform')
fieldset
each item in columns
//this loop sets up the checkboxes for each of the columns
p
input(type="checkbox", name=item, value=item)
span #{item}
br
input(type ='submit', value ='Submit')
Thanks in advance!

Sorry for waiting a month to answer my own question. Below is the code that I have. Essentially what I did was when the submit button was clicked, it called a JavaScript function that sees which checkboxes are checked and creates two hidden elements; an array of checked checkboxes and the string to place in the MySQL query (the query asks for the columns/the checkboxes that were checked).
app.post('/GetTables', function(req, res){
var columnsRequest = 'DESCRIBE ' + tableName;
var columnsList = [];
connection.query(columnsRequest, function(err, results, fields) {
if(err){
throw err;
}
for (var index in results) {
console.log(results[index].Field);
columnsList.push(results[index].Field);
}
});
//changed the line below****
var mysqlRequest = 'SELECT ' + req.body.checkboxNames + ' FROM ' + tableName;
connection.query(mysqlRequest, function(err, results, fields) {
if(err){
throw err;
}
//changed the line below********
res.render('webPageName', {'columns': columnsList, 'rows': results, 'fields': fields, 'recheckboxes':req.body.checkboxArray});
});
});
Pug/Jade
form(method = 'POST', action = '/GetTables', id = 'tableform')
fieldset
each item in columns
//this loop sets up the checkboxes for each of the columns
p
input(type="checkbox", name=item, value=item)
span #{item}
br
input(type ='submit', value ='Submit', onclick="sendCBs(document.thisForm, '/gettables')")
script.
function sendCBs(form, path, method) {
var cbNames = '';
var cbArray = [];
var count = 0;
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type == 'checkbox') {
if (form.elements[i].checked == true) {
cbNames += form.elements[i].value + ', ';
cbArray.push(form.elements[i].value);
count++;
}
}
}
if(count > 0){
cbNames = cbNames.replace(/,\s*$/, ""); //remove the last comma if 1 or more checkboxes selected
}
else{
return;
}
method = method || "POST"; //if not specified, method will be post
var inputform = document.createElement("form");
inputform.setAttribute("method", method);
inputform.setAttribute("action", path);
var hiddenField1 = createHiddenInput("checkboxNames", cbNames);
var hiddenField2 = createHiddenInput("checkboxArray", cbArray);
inputform.appendChild(hiddenField1);
inputform.appendChild(hiddenField2);
document.body.appendChild(inputform);
inputform.submit();
}

Related

Return boolean value from MYSQL in NodeJS

Im working on a function which will return a boolean-value. This value represents if an user exists in the database. Currently I have this:
function checkIfExists(){
var sql = "SELECT * FROM Users WHERE user = " + mysql.escape(req.body.username);
var rows = 0;
database.query(sql, function(err, result, fields){
console.log(result.length);
rows += result.length;
});
return rows > 0;
}
console.log(checkIfExists());
I use 'console.log(result.length)' to validate if there are results. When I test some input, I got this:
false
1
This is strange because there is one result, so rows should be equal to 1. But then it returns false instead of true. Is it possible that the value of rows isn't changed in 'database.query(...' ?
Because your function checkIfExists is asynchronous, I think you sould use callback system like this :
function checkIfExists(callback) {
var sql = "SELECT * FROM Users WHERE user = " + mysql.escape(req.body.username);
var rows = 0;
database.query(sql, function (err, result, fields) {
if (err) {
callback(err, null);
}
else {
console.log(result.length);
rows += result.length;
callback(null, rows > 0);
}
});
}
checkIfExists(function(err, isExists) {
if (err) {
// An error occured
}
else {
console.log(isExists);
}
});
EDIT
You also can simlify your checkIfExists function like this :
function checkIfExists(callback) {
var sql = "SELECT * FROM Users WHERE user = " + mysql.escape(req.body.username);
database.query(sql, function (err, result) {
callback(err, result ? result.length > 0 : false);
});
}
More information here :
Understanding Async Programming in Node.js
Hope it helps.

NodeJS and MySQL: getting array data inside query callback

I'm trying to run this piece of code:
setInterval(function () {
var params = {
QueueUrl: 'https://sqs.us-east-1.amazonaws.com/821808622769/Teste', // required
MaxNumberOfMessages: 10
};
sqs.receiveMessage(params, function (err, data) {
if (err)
console.log('Erro de SQS:' + err);
else {
var retorno = data.Messages;
for (var i in data.Messages) {
var queryString = 'SELECT now()'; //dummy query, just for tests
db_connection.query(queryString, null, function (err, rows, fields) {
if (err) {
console.log('Erro no BD:' + err);
return;
}
var date = new Date();
console.log(retorno[i].Body + ' ' + date.getTime().toString());
});
}
console.log();
}
});
}, 30000);
and I have 5 messages in AWS SQS, such as this:
but when I run my code, instead of having the retorno[i].Body of each message, sometimes I get a message repeated, as show in this image
My for loop is running from 1 to 5 queries, but how do I carry the retorno[i] inside the callback to the database query? I mean, how do I identify which message I was dealing with?
Using .bind() you can setting the internal version of i to be the same as the version of i in your loop.
.bind({i:i}) at the end of your callback and change the internal code to reference this.i
for (var i in data.Messages) {
var queryString = 'SELECT now()'; //dummy query, just for tests
db_connection.query(queryString, null, function(err, rows, fields) {
if (err) {
console.log('Erro no BD:' + err);
return;
}
var date = new Date();
console.log(retorno[this.i].Body + ' ' + date.getTime().toString());
}.bind({
i: i
});
}
}

How to save the result of MySql query in variable using node-mysql [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 1 year ago.
im trying to save the result of MySql query in variable using node-mysql model and node.js so i have this code:
connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
console.log(rows);
}
});
and the result is :
[ { idRooms: 1, Room_Name: 'dd' },
{ idRooms: 2, Room_Name: 'sad' } ]
so i need to store this results in variable so i try like this:
var someVar = connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
return rows;
}
});
console.log(someVar);
but is not working thanks for any help in advance.
Well #Fadi, connection.query is async, which mean when your call your console.log(someVar), someVar has not been set yet.
What you could do:
var someVar = [];
connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
setValue(rows);
}
});
function setValue(value) {
someVar = value;
console.log(someVar);
}
You can't do that because network i/o is asynchronous and non-blocking in node.js. So any logic that comes afterwards that must execute only after the query has finished, you must place inside the query's callback. If you have many nested asynchronous operations you may look into using a module such as async to help better organize your asynchronous tasks.
As a complement to already given answers.
var **someVar** = connection.query( *sqlQuery*, *callback function( err , row , fields){}* )
console.log(**someVar**);
This construction will return in someVar information of this connection and his SQL query . It will not return values ​​from the query .
Values ​​from the query are located in the callback function ( err , row , fields)
Here is your answer if you want to assign a variable to res.render
//before define the values
var tum_render = [];
tum_render.title = "Turan";
tum_render.description = "Turan'ın websitesi";
//left the queries seperatly
var rastgeleQuery = "SELECT baslik,hit FROM icerik ORDER BY RAND() LIMIT 1";
var son5Query = "SELECT baslik,hit FROM icerik LIMIT 5";
var query_arr = [rastgeleQuery, son5Query];
var query_name = ['rastgele', 'son5'];
//and the functions are
//query for db
function runQuery(query_arr,sira){
connection.query(query_arr[sira], function(err, rows, fields) {
if (err) throw err;
var obj = [];
obj[query_name[sira]] = rows;
sonuclar.push(obj);
if (query_arr.length <= sira+1){
sonuc();
}else{
runQuery(query_arr, sira+1);
}
});
}
//the function joins some functions http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array
function collect() {
var ret = {};
var len = arguments.length;
for (var i=0; i<len; i++) {
for (p in arguments[i]) {
if (arguments[i].hasOwnProperty(p)) {
ret[p] = arguments[i][p];
}
}
}
return ret;
}
//runQuery callback
function sonuc(){
for(var x = 0; x<=sonuclar.length-1; x++){
tum_render = collect(tum_render,sonuclar[x]);
}
console.log(tum_render);
res.render('index', tum_render);
}

Unable to get value from JSON after mySQL

var sender_username = req.session.user_id;
var recipient_username = req.body.recipient_username;
var content = req.body.content;
var sql = ' SELECT sender_username, recipient_username, COUNT(recipient_username) as count FROM message WHERE sender_username = "'+sender_username+'" AND recipient_username = "'+recipient_username+'" GROUP BY sender_username LIMIT 1 ';
var message_no = 0;
var data;
connection.query(sql, function(err, result) {
if (err) {
res.send(err);
}
else {
data = result;
// res.send(data); < - this works
// res.send(result); <- this works
// res.send(result.count); <- undefined
}
});
res.send(data); // undefined (can't seem to save to variable after connection.query())
The res.send(result); seems to work. It gives:
[{"sender_username":"sender","recipient_username":"recipient","count":2}]
I am just trying to get the value for count and save that to a variable, but things like result.count are returning undefined for some reason.
It's because the JSON is an array, so you should access it like the following
res.send(result[0].count);

How do I return the number of rows of a table according to a search criteria in Web SQL?

I have created a database called todo with table name todo having fields like title,date etc. The table is empty right now.
I have defined a function which takes title as parameter and check whether table contains that title or not.
It should return the number of rows.
GetTitle function:
todo.webdb.GetTitle = function(title) {
var db = todo.webdb.db;
db.transaction(function(tx){
tx.executeSql("SELECT title FROM todo WHERE title=?", [title],
loadTitle,
todo.webdb.onError);
});
}
loadTitle Function:
function loadTitle(tx, rs) {
return rs.rows.length;
}
GetTitle Function is called:
row=todo.webdb.GetTitle("Hello");
alert(row);
I get alert 'undefined' it should return 0
I am confused right now how to resolve this issue.
I think the transactions are asynchronous, so you need to callback function to receive the value.
todo.webdb.GetTitle = function(title, callback) {
var db = todo.webdb.db;
db.transaction(function(tx){
tx.executeSql("SELECT title FROM todo WHERE title=?", [title],
(function loadTitle(tx, rs){
callback( rs.rows.length );
}),
todo.webdb.onError);
});
}
todo.webdb.GetTitle( "Hello", function(count){
alert( "count = " + count );
});
Here's an excellent tutorial for more information.
http://blog.darkcrimson.com/2010/05/local-databases/
Update
Don't make functions inside a loop for this main reason.
Both i and title are referencing a value created out of local scope.
So the last value of assigned to both i and title will be displayed.
for (i = 0; i < itemcount; i++) {
alert('i outside if:' + i);
var title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
todo.webdb.GetTitle( title, function (count) {
if (!count) {
alert('i inside if:' + i);
alert( title );
}
});
}
Fix:
Generate a function that has the values locked in a closure.
var createTitleCallBack = function( i, title ){
return function (count) {
if (!count) {
alert('i inside if:' + i);
alert( title );
}
};
};
for (i = 0; i < itemcount; i++) {
alert('i outside if:' + i);
var title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
todo.webdb.GetTitle( title, createTitleCallBack( i, title ) );
}