I am trying to test the signup process for Social sites [google,Linkedin & Facebook] which will run concurrently using same MailId.In mysql DB,I run Queries inside the beforeLaunch:function() in CONFIG file.What is the other approach to tackle the ID=undefined issue when it executes DELETE query from DB?
Config.js file >
beforeLaunch:function(){
var ConnectDatabase = require('../dbconnections/ConnectDatabase.js');
var connectDatabase = new ConnectDatabase();
connectDatabase.connection.connect();
// checking id for created user
var sql = 'SELECT id FROM users where email='+ '\'quantra.learner#gmail.com\'';
var userID = connectDatabase.connection.query(sql,function(err, result){
if (err) {
console.log(err);
} else if (result[0].id == 'undefined' || result[0].id == 'null') {
console.log('This user is not available');
}else {
console.log('Find User ID by email : ' +result[0].id);
}
// deleting id for created user from 2 DB tables
var sql1 = 'DELETE FROM user_details WHERE user_id='+result[0].id;
var query1 = connectDatabase.connection.query(sql1,function(err, result1) {
if (err) console.log(err);
console.log('User_details Table : user_id '+ result[0].id + ' deleted!');
console.log('affectedRows: ' + result1.affectedRows);
var sql2 = 'DELETE FROM users WHERE id=' + result[0].id;
var query2 = connectDatabase.connection.query(sql2,function(err, result2) {
if (err) console.log(err);
console.log('Users Table : id ' + result[0].id + ' deleted!');
console.log('affectedRows: ' + result2.affectedRows);
});
});
});
},
Getting error > Cannot read property 'id' of undefined
Related
I am creating a nodejs module which retrieve some data from a mysql database and insert into another mysql database after some data processing. My main requirement is to make the module alive 24 hours even there is no data in the first database.. it will just keep checking for any new data. But unfortunately the module just doing nothing after few minutes of running. My function is as follows:
var to_pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'username',
password: 'password',
database: 'toDatabase',
multipleStatements: true
});
var from_pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'username',
password: 'password',
database: 'fromDatabase'
});
get_data(to_pool, from_pool);
var items_per_query = 100;
function get_data(to_pool, from_pool) {
from_pool.getConnection(function (err, from_connection) {
if (err) throw err; // not connected!
//main database query
from_connection.query("SELECT p.*, d.uniqueid as imei FROM tc_positions p left join tc_devices d on d.id = p.deviceid order by p.id desc limit " + items_per_query, function (err, result, fields) {
if (err) throw err;
var items = [];
if (Object.keys(result).length > 0) {
Object.keys(result).forEach(function (key) {
var x = result[key];
items.push({ 'id': x['id'], 'table_name': x['imei'], 'table_columns': table_columns_list });
});
}
if (items.length >= items_per_query) {
var items_to_be_removed = [];
let imei_insert = "";
let insert_data = "";
for (var x = 0; x < items.length; x++) {
let all_values = "";
let i = 0;
for (let v of Object.values(items[x]['table_columns'])) {
i++;
all_values += "'" + v + "'";
if (i < Object.keys(items[x]['table_columns']).length) {
all_values += ",";
}
}
insert_data += "INSERT INTO " + items[x]['table_name'] + "(dt_server,dt_tracker,lat,lng,altitude,angle,speed,params,fix_time,accuracy,network) VALUES(" + all_values + "); ";
items_to_be_removed.push(items[x]['id']);
if (items_to_be_removed.length == items_per_query) {
var final_query = imei_insert + ' ' + createTable + ' ' + insert_data;
to_pool.getConnection(function (err, platform_connection) {
if (err) throw err;
platform_connection.query(final_query, function (err, results, fields) {
if (err) throw err;
var ids = items_to_be_removed.join(",");
from_connection.query("DELETE FROM tc_positions where id IN(" + ids + ")", function (err, results, fields) {
if (err) throw err;
console.log('removed ' + items_to_be_removed.length + ' rows from traccar');
items_to_be_removed = [];
insert_data = "";
from_connection.destroy();
platform_connection.destroy();
// after finish all task call the same function again
return get_data(to_pool, from_pool);
});
});
});
}
}
}
else {
setInterval(function () { get_data(to_pool, from_pool); }, 10000);
}
});
});
}
the get_data() function is being called every 10 secs but the "main database query" portion never execute after sometimes. Is there any way to execute the database query again and again as the get_data() function call?
it is better to use a package manager like PM2 and start your script like this
pm2 start app.js
no need to setup intervals in your code, let the code run and exit, PM2 will restart it automatically when it stops running, this will make your code alive 24 hours as per your requirement, you can also setup delays or setup restart strategies
I am trying to store details of affectedRows from a MySQL INSERT query using NodeJS. My mind is melting trying to comprehend callbacks and Promises. Being a single-man dev team I wanted to reach out and ask for the clearest explanation as to how a callback can be applied here in a foreach loop.
The goal should be clear from these few lines of code; store data in the affected_rows[] array.
var affected_rows = [];
asset_array.forEach(function(asset) { // Populate the asset table
var query_string = "INSERT IGNORE INTO " + asset_table + " SET symbol = '" + asset[0] + "', name = '" + asset[1] + "'";
connection.query(query_string, function(err, rows, fields) {
if (err) throw err;
if ( rows.affectedRows > 0 ) {
data_to_push = [asset_table, asset[0], asset[1]];
affected_rows.push(data_to_push);
}
});
});
console.log(affected_rows); // [] for obvious async reasons
One option would be to process the asset_array inside a function and pass a callback into it and when loops through asset_array check if the current index matches the asset_array length (-1). If so call the callback.
var affected_rows = [];
function processAssets(cb) {
var array_len = asset_array_len.length
asset_array.forEach(function(asset, index) {
var query_string = 'INSERT IGNORE INTO ' + asset_table + ' SET symbol = \'' + asset[0] + '\', name = \'' + asset[1] + '\'';
connection.query(query_string, function(err, rows, fields) {
if (err) throw err
if (rows.affectedRows > 0) {
data_to_push = [asset_table, asset[0], asset[1]];
affected_rows.push(data_to_push);
}
if (index === (array_len - 1)) cb()
});
});
}
processAssets(function() {
console.log(affected_rows)
})
Will suggest you to have a look at async Queue.
You can change your code like this to use it.
//2nd Step - Perform each task and then call callback() to move to next task
var q = async.queue(function(query_string, callback) {
connection.query(query_string, function(err, rows, fields) {
if (err) throw err;
if ( rows.affectedRows > 0 ) {
data_to_push = [asset_table, asset[0], asset[1]];
affected_rows.push(data_to_push);
}
callback(); //call next task
});
}, 2); //here 2 means concurrency ie 2 tasks will run in parallel
//Final Step - Drain gives you end of queue which means all tasks have finished processing
q.drain = function() {
//Do whatever you want after all tasks are finished
};
//1st Step - create a queue of all tasks that you need to perform
for (var i = 0; i < asset_array.length ; i++) {
var query_string = "INSERT IGNORE INTO " + asset_table + " SET symbol = '" + asset[0] + "', name = '" + asset[1] + "'";
q.push(query_string);
}
Im creating a daemon that automatically changes MYSQL table contents randomly around my pages. (wordpress tables)
I have a array of stories that the system will read and then UPDATE the mysql in the tables, and as well update the timestamp on the server.
My code looks like this
//required libraries
fs = require('fs')
var mysql = require('mysql');
var dateFormat = require('dateformat');
var now = new Date();
//mysql table
var connection = mysql.createConnection({
host : 'yomamabinshoppin',
user : 'nonya',
password : 'defineltynonya',
database : 'okbye'
});
connection.connect();
//sitelisting
var sites = [ 'wp_counlwarehouseposts', 'wp_infounlwarehouseposts', 'wp_infowarehouse31posts', 'wp_netunlwarehouseposts', 'wp_netwarehouse31posts', 'wp_orgunlwarehouseposts', 'wp_orgwarehouse31posts', 'wp_stagcomwarehouseposts', 'wp_stagcowarehouseposts', 'wp_staginfwarehouseposts', 'wp_stagnetwarehouseposts', 'wp_stagorgwarehouseposts'];
//select story from catalogue
function ss (id,callback){
fs.readFile('./' + id +'.txt', 'utf8', function (err,data) {
callback(data);
});}
sites.forEach(function(entry) {
ss(Math.floor(Math.random() * 12), function (returnvalue){
fs.writeFile(entry, returnvalue);
connection.query("UPDATE `warehous_wordpress`.`"+entry+"` SET `post_date` = '"+ dateFormat(now, "yyyy-m-d") +" 01:00:01' WHERE `"+entry+"`.`ID` =1", function(err, rows, fields) {
if (err) throw err;
});
fs.appendFile('postlog.log', "UPDATE `warehous_wordpress`.`"+entry+"` SET `post_content` = '"+returnvalue+"' WHERE `"+entry+"`.`ID` = 1" , function (err) {
});
connection.query("UPDATE `warehous_wordpress`.`"+entry+"` SET `post_content` = '"+returnvalue+"' WHERE `"+entry+"`.`ID` = 1", function(err, rows, fields) {
if (err) throw err;
});
});
});
The issue in question here is at the line of
fs.appendFile('postlog.log', "UPDATE `warehous_wordpress`.`"+entry+"` SET `post_content` = '"+returnvalue+"' WHERE `"+entry+"`.`ID` = 1" , function (err) {
});
Where returnvalue is my story, and where entry is the current table name.
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual th
at corresponds to your MySQL server version for the right syntax to use near 're
frightened can become a safety issue. When designing something to scare visit'
at line 1
The story that it is referring to has the text of this.
SCARE PEOPLE THE RIGHT WAY.
"We always try to scare forward to try to keep the flow going," Travis says. "A lot of times we try to scare further down the path rather than being scared into the wall," which slows the circulation of traffic through the maze.
Plus, where people instinctively move when they're frightened can become a safety issue. When designing something to scare visitors, you have to think about how people will react—and what they might jump into if they leaped backward in terror. "You never really know how bad something is going to scare somebody," Travis explains. "We try to keep the opposite wall clear from any kind of metal props or anything like that."
At first i thought the issue was related to some html in my stories, so i removed ALL of the html in the stories, same issue was happening.
Any advice to how i could fix this?
Thank you.
UPDATE 1
After escaping the variables for the Query, the modified code, still the same parsing issue on the SQL end
//required libraries
fs = require('fs')
var mysql = require('mysql');
var dateFormat = require('dateformat');
var now = new Date();
//mysql table
var connection = mysql.createConnection({
...
});
connection.connect();
//sitelisting
var sites = [ 'wp_counlwarehouseposts', 'wp_infounlwarehouseposts', 'wp_infowarehouse31posts', 'wp_netunlwarehouseposts', 'wp_netwarehouse31posts', 'wp_orgunlwarehouseposts', 'wp_orgwarehouse31posts', 'wp_stagcomwarehouseposts', 'wp_stagcowarehouseposts', 'wp_staginfwarehouseposts', 'wp_stagnetwarehouseposts', 'wp_stagorgwarehouseposts'];
//select story from catalogue
function ss (id,callback){
fs.readFile('./' + id +'.txt', 'utf8', function (err,data) {
callback(data);
});}
sites.forEach(function(entry) {
ss(Math.floor(Math.random() * 12), function (returnvalue){
fs.writeFile(entry, returnvalue);
connection.query("UPDATE `warehous_wordpress`.`"+entry+"` SET `post_date` = '"+ dateFormat(now, "yyyy-m-d") +" 01:00:01' WHERE `"+entry+"`.`ID` =1", function(err, rows, fields) {
if (err) throw err;
});
fs.appendFile('postlog.log', "UPDATE `warehous_wordpress`.`"+ entry + "` SET `post_content` = '"+ mysql.escape(returnvalue) +"' WHERE `"+ entry +"`.`ID` = 1" , function (err) {
});
connection.query("UPDATE `warehous_wordpress`.`"+ entry +"` SET `post_content` = '" + mysql.escape(returnvalue) + "' WHERE `"+ entry +"`.`ID` = 1", function(err, rows, fields) {
if (err) throw err;
});
});
});
You need to always escape your variables correctly.
If your returnvalue is they're then this portion of your query:
SET `post_content` = '" + returnvalue + "' WHERE
will become:
SET `post_content` = 'they're' WHERE
As you can see, this will result into a syntax error at 're
In the worst case this can be used to inject some data into your database. If returnvalue e.g. would be they', ID='1, then your query will be:
SET `post_content` = 'they', ID='1' WHERE
So you always have to escape you values, either using ? or mysql.escape
Using ?? and ?:
connection.query(
"UPDATE `warehous_wordpress`.?? SET `post_content` = ? WHERE ??.`ID` = 1",
[entry, returnvalue, entry] ,
function(err, rows, fields) {});
Using mysql.escapeId and mysql.escape:
connection.query(
"UPDATE `warehous_wordpress`." + mysql.escapeId(entry) +
" SET `post_content` = " + mysql.escape(returnvalue) +
" WHERE " + mysql.escapeId(entry) + ".`ID` = 1",
function(err, rows, fields) {});
I would suggest you to use ? and ??.
Try like below
fs = require('fs');
var mysql = require('mysql');
var dateFormat = require('dateformat');
var async = require('async');
var connection = mysql.createConnection({
...
});
connection.connect();
var sites = [ 'wp_counlwarehouseposts', 'wp_infounlwarehouseposts', ...];
function copyFile(source, target, callback) {
var rs = fs.createReadStream(source);
rs.on('error', callback);
var ws = fs.createWriteStream(target);
ws.on('error', callback);
ws.on('close', callback);
rs.pipe(wr);
}
function updateSite(site, callback) {
copyFile('./' + Math.floor(Math.random() * 12) +'.txt', site, function(err) {
if (err)
return callback(err);
connection.query(
'UPDATE warehous_wordpress.? SET post_date = ? WHERE ?.ID=1',
[site, dateFormat(now, 'yyyy-m-d') + ' 01:00:01', site],
callback
);
});
}
async.eachSeries(sites, updateSite, function (err) { if (err) throw err; });
I want to know how can I save result of MySql query in variable using node js
I used this to connect to mysql and get query
var mysql = require('mysql');
var express = require('express');
var app = express();
app.get('/informations', function (req, res) {
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : '',
database : 'dbUsers',
}
);
connection.connect();
var queryString = 'SELECT * FROM hpform';
connection.query(queryString, function(err, rows, fields) {
res.json(rows);
for (var i in rows) {
console.log('NomBase: ', rows[i].NomBase);
console.log('CheminHP: ', rows[i].CheminHP);
console.log('Chemin: ', rows[i].Chemin);
console.log('HPuser: ', rows[i].HPuser);
console.log('pass: ', rows[i].pass);
console.log('path_pub: ', rows[i].path_pub);
}
});
connection.end();
});
module.exports = app;
and I have this in result:
[{"HPId":16,"NomBase":"Base","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"P:\\\\EXSTREAM\\\\BASES\\\\MACSF_DB_EditiqueV9.5.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":21,"NomBase":"LMG","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\\\hp_LMG\\\\BaseAccess20160330.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":22,"NomBase":"Meriem","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\base_meriem\\Test.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":24,"NomBase":"bouygues","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\\\hp_LMG\\\\bouygues\\\\20160425 - PRODUCTION MEP FE_22606 - Ano314002 Libellé remboursement EDP Appro","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":26,"NomBase":"hamza","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"P:\\\\EXSTREAM\\\\BASES\\\\MACSF_DB_EditiqueV9.5.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"}]
now I want to save my results as a request to use them in exec command something like this
child = exec("\"" + req.session.CheminHP + "/Packager.exe\" -APPLICATION=" + req.params.app + " -ACCESSDB=" + req.session.Chemin + " -EXSTREAMUSER=" + req.session.HPuser + " -EXSTREAMPASSWORD=" + req.session.pass + " -PACKAGEFILE=" + req.session.path_pub + "\\" + req.params.app + ".pub", function (error, stdout, stderr)
Can I have a suggestion to do this.
Thank you.
If you get only one row from your query try this:
connection.query(queryString, function(err, rows, fields) {
res.json(rows);
var elem = rows[0];
for (var prop in elem) {
req.session[prop] = elem[prop];
}
});
connection.end();
Thank you very much Nikita Namestnikov.
I used what you tell me to do and I add
console.log('NomBase: ' + req.session.NomBase);
console.log('CheminHP: ' + req.session.CheminHPBase);
console.log('Chemin: ' + req.session.CheminBase);
console.log('HPuser: ' + req.session.HPuserBase);
console.log('pass: ' + req.session.passBase);
console.log('path_pub: ' + req.session.path_pubBase);
to see if that work but there is a problem
When I Use req.session.NomBase, req.session.CheminHPBase,... in my routes it undefined
Community,
I am new at nodejs and now i have a problem i cant solve: The async in javascript/nodejs. How can i handle the following so i can push the usernames to the array?
I already tried to help myself with many different functions but nothing works for me... :/
Sincerely Adhskid.
function getCurrentBetInformations () {
connection.query('SELECT * FROM `BETS` WHERE BET_ACTIVE = "1" LIMIT 1', function(err, rowss, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
betid = rowss[0].BET_ID;
betends = rowss[0].BET_END;
connection.query('SELECT * FROM `BETS_BID` WHERE BID_BET_ID=\'' + betid + '\'', function(err, betbids, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
var betQuants = new Array();
var betIds = new Array();
var betUsernames = new Array();
var betDates = new Array();
var rowsAffected = betbids.length;
for(i=0; i < rowsAffected; i++) {
betQuants.push(betbids[i].BID_KEYS_COUNT);
betIds.push(betbids[i].BID_ID);
var betSender = betbids[i].BID_SENDER;
connection.query('SELECT `USER_NAME` FROM `USER` WHERE `USER_STEAMID` = \'' + betSender + '\' LIMIT 1', function(err, rows, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
console.log(rows[0].USER_NAME);
addUsername(rows[0].USER_NAME);
});
function addUsername (currentUsername) {
betUsernames.push(currentUsername);
}
betDates.push(betbids[i].BID_TIME);
if(betUsernames.length === i) {
execSiteRef();
}
}
function execSiteRef() {
console.log(betUsernames);
sendUserSiteRefresh([betQuants, betIds, betUsernames, betDates], betends);
}
});
});
}
I think your problem comes from this part:
if(betUsernames.length === i) {
execSiteRef();
}
You should iinstead check if the betUsernames array is of the final size:
if(betUsernames.length === rowsAffected) {
execSiteRef();
}
maybe there is more errors though, I did not check closely.