Async.js series and node-mysql query's cant get rows - javascript

I am currently trying to run a set of MySQL query's in order using async.js series control flow function. But I keep receiving the following error:
throw err; // Rethrow non-MySQL errors
^
TypeError: Cannot read property 'status' of undefined
I have tested the query's in seperate functions outside the async.series and they are fine and give me back the data, the only reason I can think for the error is due to the async nature it doesn't have the data at that time hence the error E.G when I log the rows I get:
[]
[]
[]
Below is the Async function:
function SQLuserDataAsync() {
connection.getConnection(function (err, connection) {
async.series([
function (callback) {
connection.query('SELECT status FROM users WHERE name= ?;',
[userval],
function (err, rows) {
if (rows[0]['status']) {
console.log("Account Status: " + accountval);
} else {
console.log(err);
}
callback(null, 'one');
});
},
function (callback) {
connection.query('SELECT account_type FROM settings_tbl WHERE id=(SELECT id FROM users WHERE name= ?);',
[userval],
function (err, rows) {
if (rows[0]['account_type']) {
var acctype = rows[0]['account_type'];
console.log("Account Type: " + acctype);
} else {
console.log(err);
}
callback(null, 'two');
});
},
function (callback) {
connection.query('SELECT type FROM settings_tbl WHERE id=(SELECT id FROM users WHERE name= ?);',
[userval],
function (err, rows) {
if (rows[0]['type']) {
var type = rows[0]['type'];
console.log("Type: " + type);
} else {
console.log(err);
}
callback(null, 'three');
});
}
]);
connection.release();
});
}
Any suggestions as the reason for the error or what am doing wrong here?

You've missed the main callback function to the async.series function.
function SQLuserDataAsync() {
connection.getConnection(function (err, connection) {
async.series([
function (callback) {
// YOUR CODE
},
function (callback) {
// YOUR CODE
},
function (callback) {
// YOUR CODE
}
], function(error, results) { // <--- this is the main callback
connection.release();
});
});
}
You should call connection.release() inside the main callback, otherwise, the MySQL connection will be released/terminated before the queries are executed (due to the asynchronous nature the code).

if there is a user with defined in userval name it will work.
But let's simplify our code:
function SQLuserDataAsync(userval) {
connection.getConnection(function (err, connection) {
async.waterfall([
// getting user
function (next) {
connection.query(
'SELECT * FROM users WHERE name = ? LIMIT 1',
[userval],
function (err, result) {
next(err, result[0]); // passing user to next function
});
},
// getting settings of user, maybe user_id (not id) in query below
function (user, next) {
connection.query(
'SELECT * FROM settings_tbl WHERE id = ? LIMIT 1',
[user.id],
function (err, result) {
next(err, user, result[0]);
});
},
// handling both user and settings
function (user, settings, next) {
console.log('User: ', user);
console.log('Settings: ', settings);
connection.release();
}
]);
});
}
SQLuserDataAsync('someone');

Related

how to get the new value of document after updating each time in nodejs loop?

I'm trying to update collection many times in a loop but each time I get the value of oldRight not changed it gives the first value always.
I'm storing topics in mongodb using nested sets pattern.
for(let name of level1) {
console.log(name);
var parent = 'root';
getParentAndAddTopic(name, parent, function (err, topic) {
if (err) {
console.log(err);
}
var oldRight = topic.right;
console.log(oldRight);
db.topics.create({name: name, parent: 'root', left: oldRight, right: oldRight + 1}
, function (err, res) {
if (err) throw err;
});
db.topics.updateMany({right: {$gte: oldRight}}, {$inc: {right: 2}}
, function (err, res) {
if (err) throw err;
});
});
function getParentAndAddTopic(name, parent, callback) {
db.topics.find({name: 'root'}, function (err, topics) {
if (err) {
callback(err, null);
} else {
callback(null, topics[0]);
}
});
}
}
Working with the database is an asynchronous function, you can't do it in a normal loop. And change var to let, using var is a bad practice leading to many problems.

callback problem - callback is not a function

I got an error like that: uncaughtException TypeError: cb is not a function
I think this error is caused by callback error but I don't know why I have this error.
app.put('/badge/student_badge/:id', upload, (req, res, next) => {
const name = req.body.name;
let data = {
name: name
}
badger.updatePersonBadge({
id: req.params.id
}, data, (err) => {
if (err) return next(err);
res.status(201).json({
message: 'Post updated successfully!'
});
});
});
function updatePersonBadge(options, cb) {
schemas.badger.then(b => {
b.findById({
_id: options.id
}, (err, resp) => {
if (err) return cb(err);
if (!resp) return cb("no badge found");
name = options.name;
title = resp.title;
points = resp.points;
updateBadge(name, title, points, cb);
cb(null, resp);
})
})
}
function updateBadge(name, title, points, cb) {
const dateCreated = new Date(),
dateUpdated = dateCreated;
registerSchemas.personModel.then(p => {
p.findOneAndUpdate({
name: name
}, {
$push: {
badges: [{
title: title,
points: points,
dateCreated: dateCreated,
dateUpdated: dateUpdated
}]
}
}, (err, resp) => {
if (err) return cb(err);
if (!resp) return cb("no person found");
})
})
}
You are not passing the cb argument and if it's optional (at least seems it should be) the function misses an if statement:
updatePersonBadge(options, cb) { // << cb (callback) argument expected
// ...
cb(null, resp); // cb called therefore not optional (Its needed)
If you use it like updatePersonBadge(aaa) instead of updatePersonBadge(aaa, myCallbackFn) the cb() is undefined but expressed as a function call - that does not exist.
You could instead make it optional (if that's the case):
//...
if(cb) cb(null, resp); // call the cb function if cb argument exists
or if you want to be more specific:
//...
if(cb && typeof cb === 'function') cb(null, resp);
instead of passing a function you're passing data:
badger.updatePersonBadge({}, data, errFn);
I assume this is the place from where you are calling updatePersonBadge. IF yes, then you are passing callback as a third argument, You have to use them correctly.
badger.updatePersonBadge(
{
id: req.params.id
},
data,
(err) => {
if (err) return next(err);
res.status(201).json({
message: 'Post updated successfully!'
});
});
In the example the problem is parameter mismatch, In place of callback you send data
app.put('/badge/student_badge/:id', upload, (req, res, next) => {
const name = req.body.name;
let data = {
name: name
}
badger.updatePersonBadge({id:req.params.id}, data, (err)=>{. -- three arguments passed
if (err) return next(err);
res.status(201).json({
message: 'Post updated successfully!'
});
});
});
Where in function definition you have only 2 parameters defined.
where it should be 3 parameters / that particular scenario should be validated.

how to make synchronous http calls within async.each in nodejs

I want to make http requests to an API-s to collect for each user it's data and insert into mongodb.
The problem I am having is, it is doing all the requests at once, and seems it gets stuck somewhere and I don't know what is going on.
Al thou I am using async library and add the request() method inside each iteration, and I dont know if this is the right way, here is the code:
function iterateThruAllStudents(from, to) {
Student.find({status: 'student'})
.populate('user')
.exec(function (err, students) {
if (err) {
throw err;
}
async.forEach(students, function iteratee(student, callback) {
if (student.worksnap.user != null) {
var options = {
url: 'https://api.worksnaps.com/api/projects/' + project_id + '/time_entries.xml?user_ids=' + student.worksnap.user.user_id + '&from_timestamp=' + from + '&to_timestamp=' + to,
headers: {
'Authorization': 'Basic bGhNSVJkVUFwOE1DS2loOFVyZkFyOENEZEhPSXdCdUlHdElWMHo0czo='
}
};
request(options, getTimeEntriesFromWorksnap);
}
callback(); // tell async that the iterator has completed
}, function (err) {
console.log('iterating done');
});
});
}
function getTimeEntriesFromWorksnap(error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
parser.parseString(body, function (err, results) {
var json_string = JSON.stringify(results.time_entries);
var timeEntries = JSON.parse(json_string);
_.forEach(timeEntries, function (timeEntry) {
_.forEach(timeEntry, function (item) {
saveTimeEntry(item);
});
});
});
}
}
function saveTimeEntry(item) {
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.exec(function (err, student) {
if (err) {
throw err;
}
student.timeEntries.push(item);
student.save(function (err) {
if (err) {
console.log(err);
} else {
console.log('item inserted...');
}
});
});
}
var from = new Date(startDate).getTime() / 1000;
startDate.setDate(startDate.getDate() + 30);
var to = new Date(startDate).getTime() / 1000;
iterateThruAllStudents(from, to);
I am new to JavaScript, especially when dealing with async.
Any help?
Use Async.eachLimit() to make batched request to the api...Try this iterateThruAllStudents() function.
I already had same question before here
See tutorial of limiting here.
Though i am making the limit as 5 but you can do whatever you want(10,50 etc).
function iterateThruAllStudents(from, to) {
Student.find({status: 'student'})
.populate('user')
.exec(function (err, students) {
if (err) {
throw err;
}
async.eachLimit(students,5,function iteratee(student, callback) {
if (student.worksnap.user != null) {
var options = {
url: 'https://api.worksnaps.com/api/projects/' + project_id + '/time_entries.xml?user_ids=' + student.worksnap.user.user_id + '&from_timestamp=' + from + '&to_timestamp=' + to,
headers: {
'Authorization': 'Basic bGhNSVJkVUFwOE1DS2loOFVyZkFyOENEZEhPSXdCdUlHdElWMHo0czo='
}
};
request(options,getTimeEntriesFromWorksnap(callback));
}
}, function (err) {
console.log(err);
console.log('iterating done');
});
});
}
function getTimeEntriesFromWorksnap(cb) {
return function(error, response, body){
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
parser.parseString(body, function (err, results) {
var json_string = JSON.stringify(results.time_entries);
var timeEntries = JSON.parse(json_string);
async.each(timeEntries,function(timeEntry,cb1){
async.each(timeEntry,function(item,cb2){
saveTimeEntry(item,cb2);
},function(err){
if(err)
cb1(err);
else
cb1();
})
},function(err){
if(err)
cb(err);
else
cb();
});
//_.forEach(timeEntries, function (timeEntry) {
// _.forEach(timeEntry, function (item) {
// saveTimeEntry(item);
// });
//});
});
}
cb(null);
}
}
function saveTimeEntry(item,cb2) {
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.exec(function (err, student) {
if (err) {
return cb2(err);
}
student.timeEntries.push(item);
student.save(function (err) {
if (err) {
console.log(err);
//return cb2(err);//Do it if you wanna throw an error.
} else {
console.log('item inserted...');
}
cb2();
});
});
}
var from = new Date(startDate).getTime() / 1000;
startDate.setDate(startDate.getDate() + 30);
var to = new Date(startDate).getTime() / 1000;
iterateThruAllStudents(from, to);
In your example you missed iteratee param in the each method of async - iteratee(item, callback). Look at this example here.
You need to call callback each time inside your iteratee function to tell async continue doing its processing.
each(collection, iteratee, [callback])
collection - collection to iterate over.
iteratee(item, callback) - function to apply to each item in coll. The iteratee is passed a callback(err) which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit null argument. The array index is not passed to the iteratee. If you need the index, use forEachOf.
callback(err) - Optional callback which is called when all iteratee functions have finished, or an error occurs.
If you need synchronous behavior, no probs! There is also eachSeries method with the same signature except every collection item will be iterated synchronously.
UPDATE:
Changes should be implemented:
Pass async callback:
request(options, getTimeEntriesFromWorksnap(callback));
Return necessary for request callback function:
function getTimeEntriesFromWorksnap(callback) {
return function(error, response, body) {
// ...
saveTimeEntry(item, callback);
// ...
}
}
Call callback only after record is saved in database:
function saveTimeEntry(item, callback) {
// ..
student.save(callback);
// ..
}
Refactor nested loops (not sure what timeEntries, timeEntry are, so use appropriate async method to iterate these data structures):
async.each(timeEntries, function (timeEntry, callback) {
async.each(timeEntry, function (item, callback) {
saveTimeEntry(item, callback);
}, callback);
}, callback);

synchronous queries on oracledb using aysync in nodejs

i am very new to nodejs. I am using oracledb and want to make the queries in synchronous order. So once 1st query is executed then output of the query will be used in second query. i looked at async.waterfall and wrote below code. is it correct way to call function in synchronous way ? Thanks in Advance. it may be possible that 1st query takes more time than second.
var update = function(fnParam1, fnParam2){
oracledb.maxRows = 10000;
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, connection)
{
if (err) { console.log(err.message); return; }
async.waterfall([
function(callback) {
connection.execute("select column1 from table1 where param1 =:nm",[fnParam1],
function(err, result)
{
if (err) { console.log(err); return; }
column1 = String(result.rows[0][0]);
});
callback(null, column1,connection);
},
function(column1, connection,callback) {
connection.execute("select count(*) from table2 where column1 = :par1 and column2= :par2",[column1,fnParam2],
function(err, result)
{
if (err) { console.log(err); return; }
var count = String(result.rows[0][0]);
console.log("count result:" + count)
});
callback(null, count);
}
],
function (err, result) {
console.log("Done" + result);
});
});
};
Just taking a quick look at the code, I noticed your "callback" invocations are not in the callback functions that are passed to execute. This means your going to the next step in your waterfall before you have the results from the execute method. Here's your code slightly modified to demonstrate what I mean. Also, don't forget to release your connection when you're done with it.
var update = function(fnParam1, fnParam2){
oracledb.maxRows = 10000;
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, connection)
{
if (err) { console.log(err.message); return; }
async.waterfall([
function(callback) {
connection.execute("select column1 from table1 where param1 =:nm",[fnParam1],
function(err, result)
{
if (err) { console.log(err); return; }
column1 = String(result.rows[0][0]);
callback(null, column1, connection);
});
},
function(column1, connection, callback) {
connection.execute("select count(*) from table2 where column1 = :par1 and column2= :par2",
[column1,fnParam2],
function(err, result)
{
if (err) { console.log(err); return; }
var count = String(result.rows[0][0]);
console.log("count result:" + count);
callback(null, count);
});
}
],
function (err, result) {
console.log("Done" + result);
});
});
};

How to wait for a response from a mongo findOne query in a node/express app before using the response in following control flow

I am new to node, and also JavaScript callbacks.
I am trying to check if an account exists in mongo and then 'save it' if it doesn't and return an error if it does.
I am currently trying to figure this out outside of my express app. This is what i have..
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/main', function (err, db) {
if(err) throw err;
var query = { name : "www.website.com"}
findOne(db, query, function (doc) {
if(doc) {
console.log('account exists');
} else {
console.log('good to go');
}
console.dir(doc);
});
});
var findOne = function (db, query, callback) {
db.collection('accounts').findOne(query, function (err, doc) {
if(err) throw err;
db.close();
callback();
});
}
with the console.dir(doc); above returning as undefined. How do I wait for the findOne to return before using the callback to console.log or save the account?
The reason you are getting undefined is because when you call your callback your are not passing it the doc. That line should look like callback(doc).
Here is an updated version of your code with a few suggestions:
MongoClient.connect('mongodb://localhost:27017/main', function (err, db) {
if(err) throw err;
var query = { name : "www.website.com"}
findOne(db, query, function (err, doc) {
if(err) {
// something went wrong
console.log(err);
return;
}
if(doc) {
console.log('account exists');
console.dir(doc);
} else {
console.log('good to go');
}
});
});
var findOne = function (db, query, callback) {
db.collection('accounts').findOne(query, function (err, doc) {
db.close();
if(err) {
// don't use throw when in async code
// the convention is to call your callback with the error
// as the first argument (notice that I added an argument
// to the definition of your callback above)
callback(err);
}
else {
// call your callback with no error and the data
callback(null, doc);
}
});
}

Categories

Resources