thank you for taking the time to read my question.
I have an array with records that I want to put into MYSQL database from NodeJS. These records have a jobkey field that I want to be UNIQUE, but I don't want it to be the PRIMARY KEY since the jobkey coming from an API and I want to use my own recid for better indexing and sorting.
I am using NodeJS to first check if the jobkey exists by doing a SELECT WHERE and checking if rows.length > 0? then adding the job if jobkey does not already exist. For some reason I keep getting a ER_DUP_ENTRY error and I can't figure out why my rows.length sometimes === 0 even though the jobkey exists. I have been hitting my head against the wall now for a few days. I hope it's not just a semi-colon or one of those simple errors I have overlooked. Anyways I need help please look at my code.
function is_job_key(_jobkey, cb){ // cb(err, flag)
var sql = 'SELECT * FROM tbl_indeed WHERE ?';
conn.query(sql, {jobkey: _jobkey}, function(err, rows){
if(err){
console.log(err);
return cb(true);
}
bar.tick();
if(rows.length > 0){ // jobkey exists
return cb(null, true);
}
cb(null, false);
});
}
function insert_job(posts, cb){ // cb(err)
var sql = 'INSERT INTO tbl_indeed SET ?';
conn.query(sql, posts, function(err, result){
if(err){
console.log(err);
return cb(true);
}
bar.tick();
cb(null);
});
}
function insert_all_jobs(job_arr, query_str, cb){ // cb(err, count_inserted)
function get_posts(job){
var posts = {jobkey: job.jobkey, snippet: job.snippet, url: job.url, jobtitle: job.jobtitle, company: job.company, date: job.date, query_string: query_str};
return posts;
}
bar = new progress(' ' + query_str + ' (:current / :total) [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 40,
total: job_arr.length*2
});
var count_inserted = 0;
async.each(job_arr, function(job, cb){
var posts = get_posts(job);
is_job_key(job.jobkey, function(err, result){
if(err) return cb(true);
if(result){
bar.tick();
return cb(); // jobkey exists
}
insert_job(posts, function(err){
if(err) return cb(true);
count_inserted++;
cb();
});
});
}, function(err){
if(err) return cb(true);
console.log('done');
conn.end();
cb(null, count_inserted);
});
}
var job_arr = JSON.parse(fs.readFileSync('technician.txt', 'utf-8'));
//job_arr = job_arr.splice(1, 10); //for some reason this works ok...
insert_all_jobs(job_arr, 'technician', function(err, result){
if(err) return console.log('error');
console.log(result);
});
thank you for reading my question and have a lovely day
~A
Related
im writing a discord bot to check points from a database then return them. This is my code at the moment.
function userCheck(id, name) {
date = new Date().toISOString().slice(0, 19).replace('T', ' ');
con.query("SELECT id FROM users WHERE id = "+id, function (err, row) {
if (!(row && row.length) ) {
con.query("INSERT INTO `users` (id,name,rank,points,signup) VALUES ('"+id+"', "+name+", '0', '0' , '"+date+"')"), function (err, result, fields){
if (err) throw err;
}
}else{
//fine
}
});
}
function checkPoints(user){
id = user.id;
name = con.escape(user.username);
userCheck(id, name);
console.log("SELECT points FROM users WHERE id = "+id);
con.query("SELECT points FROM users WHERE id = "+id, function (err, result, fields){
return result[0].points;
});
}
My code which calls these functions is this:
return message.author.send(checkPoints(message.author));
This makes discordjs error as it is trying to return an empty message. This means my functions arent returning right. Ive look at this over and over its probably just a simple fix but I cant see it.
Thanks in advance
function checkPoints(user){ doesn't return anything i.e. same as return undefined
Since con.query is asynchronous - the simplest fix is to use a callback, like so
function checkPoints(user, cb){
id = user.id;
name = con.escape(user.username);
userCheck(id, name);
console.log("SELECT points FROM users WHERE id = "+id);
con.query("SELECT points FROM users WHERE id = "+id, function (err, result, fields){
cb(result[0].points);
});
}
checkPoints(message.author, function(result) {
message.author.send(result);
});
or, use Promise
function checkPoints(user){
return new Promise(function(resolve, reject) {
id = user.id;
name = con.escape(user.username);
userCheck(id, name);
console.log("SELECT points FROM users WHERE id = "+id);
con.query("SELECT points FROM users WHERE id = "+id, function (err, result, fields){
if(err) return reject(err);
resolve(result[0].points);
});
});
}
checkPoints(message.author)
.then(function(result) {
message.author.send(result);
});
connection.query('select * from `test` WHERE `deny`=false',function(err,rows,fields){
for (var i=0; i<rows.length; i++){
if (rows[i].enddate == mydate)
{
msg = "message";
connection.query('select * from `cash` WHERE `id`=test',function(err2,rows2,fields2){
if (rows2[0].cash >= 30)
{
connection.query('UPDATE `cash` SET `cash`='+ rows2[0].cash-30 +' WHERE `id`=test',function(err3,rows3,fields3){
msgsend(rows[i].contact,msg);
});
}
});
}
}
});
This is my code.
However, when I run my code
It shows an error like this:
TypeError: Cannot read property '0' of undefined
Please help
What is wrong with my code?
Try Debugging it firstly using
console.log(rows) and console.log(rows2) may be one of them is empty.
Also the error parameters are for reason ,you could catch error parameters and check them:
if(err) console.log(err)
else //rest of program
Similarly err2
if(err2) console.log(err2)
else //rest of program
This will definetly help in solving the issue
EDIT
Where do you put debug line??
Obviously where you have to debug
Like
function(err,rows,fields){
console.log(rows); //undefined if empty
if(err) console.log(err);
else //rest code
}
Have you tried Async
connection.query('select * from `test` WHERE `deny`=false',function(err,rows,fields) {
for (var i=0; i<rows.length; i++){
if (rows[i].enddate == mydate) {
myFuntion(row[i], function(msg) {
console.log(msg) // Success!
})
}
}
})
function myFuntion(row, callback) {
async.waterfall([
function(callback) {
msg = "message";
connection.query('select * from `cash` WHERE `id`=test',function(err2,rows2,fields2){
callback(null, rows2);
});
}, function(rows2, callback) {
if (rows2[0].cash >= 30)
{
connection.query('UPDATE `cash` SET `cash`='+ rows2[0].cash-30 +' WHERE `id`=test',function(err3,rows3,fields3){
msgsend(row.contact,msg);
callback(null);
});
}
}
], function(err){
if(err)
console.log("Error: ", err);
else
callback("Success!");
})
}
The code is for handling the POST request within Expressjs and mongodb
router.post('/', function(req, res){
var data = req.body;
Tag.find({name: data.name}).limit(1).exec( function(err, result){
if(err){
} else {
if(result.length > 0){ // Already exist a tag with same name
res.status(400).end('Already exist!');
} else { // Save the new Tag to database
var tag = new Tag();
tag.name = data.name;
tag.lastModifier = req.user?req.user.username:"system";
tag.lastModified = Date.now();
tag.save(function(err){
if(err){
res.status(400).json({
message: "insert tag error"
});
} else {
Tag.findOne(tag, function(err, result){
if(err){
res.status(400).json({
message: "some error.."
});
} else {
//res.status(400).end('same tag name');
res.status(201).json({
_id: result._id
});
}
});
}
});
}
}
});
});
The stairs in the last 9 lines are terrible....please teach me how could I make this mess clearer?
You can use named functions instead of some of the function expressions:
router.post('/', function(req, res){
var data = req.body;
Tag.find({name: data.name}).limit(1).exec( function(err, result){
if(err){
} else {
if(result.length > 0){ // Already exist a tag with same name
res.status(400).end('Already exist!');
} else { // Save the new Tag to database
var tag = new Tag();
tag.name = data.name;
tag.lastModifier = req.user?req.user.username:"system";
tag.lastModified = Date.now();
tag.save(save(err));
}
}
});
});
function save(err){
if(err){
res.status(400).json({
message: "insert tag error"
});
} else {
Tag.findOne(tag, handleResult(err, result));
}
}
function handleResult(err, result){
if(err){
res.status(400).json({
message: "some error.."
});
} else {
//res.status(400).end('same tag name');
res.status(201).json({
_id: result._id
});
}
}
(You can surely name them a little more appropriate for the situation, but it shows the principle.)
router.post('/', function(req, res){
var data = req.body;
Tag.find({name: data.name}).limit(1).exec(cbExec);
});
function cbExec(err, result){
if(err){
} else {
if(result.length > 0){ // Already exist a tag with same name
res.status(400).end('Already exist!');
} else { // Save the new Tag to database
var tag = new Tag();
tag.name = data.name;
tag.lastModifier = req.user?req.user.username:"system";
tag.lastModified = Date.now();
tag.save(cbSave);
}
}
}
function cbSave(err){
if(err){
res.status(400).json({message: "insert tag error"});
} else {
Tag.findOne(tag, cbTag);
}
}
function cbTag(err, result){
if(err){
res.status(400).json({message: "some error.."});
} else {
//res.status(400).end('same tag name');
res.status(201).json({_id: result._id});
}
}
I really recommend you to try promises. There are many implementations available for JavaScript and Node.js.
A promise basically encapsulates an asynchronous operation into a value, which allows you to get rid of these horrible nested callbacks. They also allow you to chain asynchronous operations more easily.
What you're forced to do in your callback-based code is to check errors at every level, which can get rather tedious if your error handling could be at one place. Promises will propagate the error, allowing easy handling in one place.
Here are some references:
http://www.html5rocks.com/en/tutorials/es6/promises/
https://developer.mozilla.org/cs/docs/Web/JavaScript/Reference/Global_Objects/Promise
https://www.promisejs.org/
It might take a little while to adjust to using them, but trust me, it is absolutely worth it.
You can separate the cod a little bi more. Instead of creating lambda functions create normal ones. You can get rid of one pair of braces in 4th line
if(err){
} else {
using if(!err)
Edit: SELECT * query is returning my dates in an incorrect format. Using the mysql2 module to execute a connection.query(), pass all data to a server-side variable, and calling it client-side using AJAX.
router.post('/applicants', function(req, res){
connectionpool.getConnection(function(err, connection){
if(err){
console.error('CONNECTION error: ', err);
res.statusCode = 503;
res.send({
result: 'error',
err: err.code
});
} else {
console.log
connection.query('SELECT * FROM applicant',
function(err, rows){
if (err) console.error(err);
res.send(rows);
}
);
connection.release();
}
});
});
var applicants = '';
$.post('/applicants').done(function(data){
applicants = data;
});
$('.name').click(function(){
var selected = $(this).attr('id');
applicants.forEach(function(applicant){
if(selected == applicant.applicant_id){
...
$('input[name="applicant_dob"]').val(applicant.applicant_dob);
I can't parse it out by '-' because it returns the day as 02T05:00:00.000Z instead of 02. It looks correct in phpmyadmin. Any ideas?
you are looking at at a field with type "datetime with timezone"
you could modify the output in your query using the date_format() function of mysql.
I have this Node.js method for GET queries
app.get('/:tableName', function(req, res) {
var schema = require('./schema/' + req.param('tableName'));
var q = unescape(Object.keys(req.query)[0]);
console.log('query:' + q);
schema.find(q, function(err, result) {
if (err) {
console.log(err);
res.status(500).json({status: 'failure'});
}
if (result) {
res.json(result);
} else {
res.json({status: 'not found'});
}
});
});
The strage, that if I call it i.e. whit this input .. (it is exactly the value of var q)
{
"remoteID" : "80E75ED5-B696-446E-9AA7-9B170350D7E6"
}
.. I get back 3 items but non of them match the condition, basically it returns all the items in table.
[{"lastName":"K","remoteID":"D5DC51C1-C415-4073-A647-1D41BB47D03E","password":"p","firstName":"J","email":"xxx","airline":"73C02335-C019-419E-BCC7-C87537F38492","userName":"J","_id":"5353b639889a61000038848c","__v":0},
{"lastName":"asd","remoteID":"C471495C-C218-4440-BA81-7BD37CBB5605","password":"wn831","firstName":"asd","email":"asd#asd.hu","airline":"73C02335-C019-419E-BCC7-C87537F38492","userName":"ASA","_id":"5353b639889a610000388491","__v":0},
{"remoteID":"5A0D51DE-DCD0-4E3E-9D97-7CAD066DB68A","userName":"R","password":"R","_id":"5353b639889a610000388492","__v":0}]
One important line was missing:
var jq = JSON.parse(q);
schema.find(jq, function(err, result) {
find() method requires not string but JSON object.