async.js methods inside async.js methods - node.js

Disclaimer: new to JS and everything around it. :)
I was wondering if we could nest async functions within one another
I have a waterfall method in node:
/**
* POST new travel wish
*/
router.post('/add', function(req, res) {
var db = req.db;
console.log(req.body);
// add properties: customer_id and timestamp
var date = new Date();
//...
var notif;
async.waterfall(
[
// find the User
function(next){
db.collection(userCollection).findOne({_id: new ObjectId(req.session.userID)}, next);
},
// insert a new travelwish
function(user, next){
notif = {
'username': user.username,
};
// TODO: add a async.parallel
db.collection('travelwishlist').insert(req.body, next);
},
// setup timer for processing and send confirmation
function(insertedTravelWish, next){
NotificationManager.sendConfirmationNotification(notif);
next(null, insertedTravelWish);
}
],
// waterfall callback --> send http response.
function (err, insertedTravelWish) {
res.send(
//if everything is ok the system sends the travel wish ID to the client otherwise error
(err === null) ? { msg: insertedTravelWish[0]["_id"] } : { msg: 'err' }
);
}
);
});
I want to insert multiple items instead of just one.
I think i can do this with parallel, but can I put it inside the existing waterfall?

Yep, you totally can do it, here is some piece of example
async.waterfall([
function (callback) {
callback(null, 1);
},
function (arg, callback) {
callback(null, arg1 + 1);
},
function (arg, callback) {
async.parallel([
function (_callback) { ... },
function (_callback) { ... },
], finalCallback);
}
]);
just a little tip, make sure you proivde different names for the callbacks (so you won't call the other one) if I've 2 nested functions that requires a callback most of the time I name them as callback _callback __callback

Related

res is not defined in Node.js (Mongoose)

How can I put res in a normal function i.e not an exported one which is not part of routes?
function createNewStudent(v,callBackOne){
if (callBackOne) {
studentInfo.callBackOneStudent = callBackOne;
}
// common filter json
var filterjson = common.defaultFilterJson();
filterjson['active'] = true;
filterjson['email'] = v.email;
// student initialization
var student = new Student(v);
async.waterfall([
function (done) {
student.save(function (err) {
if (!err) {
studentInfo.callBackOneStudent();
Employee.update({_id: student.created_by},{"$push": { "students": student._id } }).exec(function (err, employee) { });
done();
}
});
}
}
});
},
function (done) {
var url = config.mailer.studentActivateUrl + student._id;
---error is here-----
res.render('modules/users/server/templates/student-confirmation-email', {
name: student.first_name + ' ' + student.last_name,
appName: 'GAIPP',
url: url
}, function (err, emailHTML) {
done(err, emailHTML, student);
});
}
});
My error is 'res' is not defined. Can anyone please help me to solve this error?
The only way that you can put res in a function is if you somehow supply it to that function at runtime. Remember that res is meaningful only in request handling. Outside of the request handler your function couldn't even know which request to respond to because there might be several requests served at the same time.
If you want to have a function that has access to res then you have those options:
Use a nested function in your request handler, e.g.
app.get('/foo', function (req, res) {
function x() {
// you can use res here
}
x();
});
Add res as an argument:
function x(res) {
// you can use res here
}
app.get('/foo', function (req, res) {
x(res);
});
Another option would be to add a callback to your function that would be passed by the handler:
function x(args, cb) {
// you cannot use res here
// but you can call the callback:
cb(null, 'something');
}
app.get('/foo', function (req, res) {
x(function (err, data) {
if (err) {
// handle error
}
// use res here with data supplied by x()
res(data);
});
});
Instead of using callback your x() function could also return a promise.

Fetch from multiple, separate, collections with Express and MongoDB

I have a site where i need some data from my Mongo data to be shown. My problem, however, is that i need data from two collections. Collections that are completely separate and have nothing to do with each other.
Right now i have this in my routes for my profile-page:
router.get('/profile', function(req, res,next) {
var resultArray = [];
mongo.connect(url, function(err, db) {
var cursor = db.collection('users').find();
cursor.forEach(function(doc, err) {
resultArray.push(doc);
}, function() {
db.close();
res.render('profile/index', {users: resultArray});
});
});
});
And this, of course, works perfectly fine. But how do i get a second db.collection('colors').find(); to be passed along to my template too?
I'm sure it's something trivial, and me just not quite having the full grasp of things, but yeah.. I'm stuck..
Use the async library which is best suited for this scenario. Where you need to run multiple tasks that do not depend on each other and when they all finish do something else, you should use async.parallel() method. The signature is async.parallel(tasks, callback), where tasks is an array of functions.
It will immediately run all the functions in parallel, wait for all of them to call their task callback, and finally when all tasks are complete it will run callback (the final callback).
The following example demonstrates how this could be adapted for your use case:
router.get('/profile', function(req, res, next) {
mongo.connect(url, function(err, db) {
var locals = {};
var tasks = [
// Load users
function(callback) {
db.collection('users').find({}).toArray(function(err, users) {
if (err) return callback(err);
locals.users = users;
callback();
});
},
// Load colors
function(callback) {
db.collection('colors').find({}).toArray(function(err, colors) {
if (err) return callback(err);
locals.colors = colors;
callback();
});
}
];
async.parallel(tasks, function(err) { //This function gets called after the two tasks have called their "task callbacks"
if (err) return next(err); //If an error occurred, let express handle it by calling the `next` function
// Here `locals` will be an object with `users` and `colors` keys
// Example: `locals = {users: [...], colors: [...]}`
db.close();
res.render('profile/index', locals);
});
});
});
Try this code:
router.get('/profile', function(req, res,next) {
var resultArray = {
users : [],
colors : []
};
mongo.connect(url, function(err, db) {
var cursor = db.collection('users').find();
cursor.forEach(function(doc, err) {
resultArray.users.push(doc);
}
var colors = db.collection('colors').find();
colors.forEach(function(doc,err){
resultArray.colors.push(doc);
}, function() {
db.close();
res.render('profile/index', {users: resultArray.users, colors: resultArray.colors});
});
});
});
Didn't have time to check it, but I'm pretty sure that it would work.

mocha test function with mongoose call

Having some problems unit testing the below code, I'm unsure whether it is possible or not due to the way it is coded.
storeModel.js
var storeSchema = new Schema({
storeId : { type: String, index: true},
storeName : String
});
var model = mongoose.model('store', storeSchema);
var findStoresById = function(ids, callback) {
model.find({ storeId: { $in: ids }}, function (err, result) {
if (err) callback(err);
callback(err, result);
});
};
return {
findStoresById: findStoresById,
schema: storeSchema,
model: model
};}();
Which i test like so..
it('will call "findStoresById" and return matched values [storeId: 1111] ', function (done) {
storeModel.findStoresById(['1111'], function(err, store) {
assert.equal(store[0].storeId, '1111');
assert.equal(store[0].storeName, 'StoreName');
assert.equal(err, null);
done();
});
});
However the problem when i implement the following code within a separate function:
get: function (req, res) {
if (req.query.storeIds) {
var ids = req.query.storeIds.split(',');
storeModel.findStoresById(ids, function(err, stores) {
if (err) {
return res.send(err);
}
if (_.isEmpty(stores)) {
var error = {
message: "No Results",
errorKey: "XXXX"
}
return res.status(404).json(error);
}
return res.json(stores);
}); ...
How can i unit test this, i dont want to mock it as there is functionality in the "findStoreById" that needs testing, or is a refactor needed? suggestions?
I'd contest that you actually should be stubbing findStoreById because in not doing so get cannot strictly be unit tested, in that it's not isolated and could fail through no fault of its own. Seeing as the functionality you'd want to test lies within the callback of findStoreById and not the method itself, we can happily stub it and use the yields method of sinon to invoke its callback accordingly.
Note that, if you're testing routes, it's preferable to use supertest else you'll have a lot of mocking of request and response methods on your hands. Therefore, for example:
var request = require('supertest');
var express = require('express');
// stub database method
sinon.stub(storeModel, 'findStoresById');
// create a test app/route to which we direct test requests
var app = express();
app.get('/', myRouteFunction);
it('sends a 404 error when no stores are found', function(done) {
// use the second argument of `yields` to pass a result to the callback
storeModel.findStoresById.yields(null, []);
request(app).get('/').expect(404, done);
});
it('responds with any stores found', function(done) {
// pass an array of found stores to the callback
storeModel.findStoresById.yields(null, [{_id: 1}]);
request(app).get('/').end(function(err, res) {
if(err) return done(err);
assert.deepEqual(res.body, [{_id: 1}]);
done();
});
});
If what you want is test static's and method's of certain Mongoose model, I would recommend you to use sinon and sinon-mongoose.
But first, some tips for your code
var storeSchema = new Schema({
storeId : { type: String, index: true},
storeName : String
});
// 1) If you will callback with the same 'err' and 'result', pass the callback directly
function findStoresById(ids, callback) {
// Instead of this...
this.find({ storeId: { $in: ids } }, function (err, result) {
if (err) callback(err);
callback(err, result);
});
// Use this... it's the same, but shorter
this.find({ storeId: { $in: ids } }, callback);
}
// 2) Declare a static method on your model, instead of export manually (see Monggose documentation for more info)
storeSchema.static('findStoresById', function (ids, callback) {
});
// 3) Create your model after the statics were declared, and use CamelCase
var model = mongoose.model('Store', storeSchema);
// 4) Export just your model
// If you want the model -> var Store = mongoose.model('Store')
// If you want the schema -> var schema = Store.schema
// If you want to call your method -> Store.findStoresById(...)
module.exports = model;
Then, to test the method findStoresById
var sinon = require('sinon');
require('sinon-mongoose');
var Store = mongoose.model('Store');
sinon.mock(Store)
.expects('find').withArgs({ storeId: { $in: ['id1', 'id2'] } })
.yields(null, 'SUCCESS!');
Store.findStoresById(['id1', 'id2'], function (err, res) {
assert(res, 'SUCCESS!');
});
You can find working (and simple) examples on the sinon-mongoose repo.

nodejs async execute callbacks in series

I have a situation like:
function smth(data) {
// save data to db.
Object.findOne({ _id: ceva['id']}, function(obj) {
obj.save();
});
}
This function is called from various async calls. There is a race condition where the second findOne call runs before a previous save() runs.
Is there a way to work around this? Maybe using the async library to run things in series?
You can make use one of async control flows to ensure every iteration of smth() happens in order.
If you're not in favor of using a flow control library, you can easily achieve series execution of each event. Consider following code snippet:
function smth(data, cb) {
// save data to db.
findOne({
id: data.id
}, function (err, obj) {
if (!err && obj) {
savedb(obj, cb);
} else {
cb(err);
}
});
}
function findOne(filter, cb) {
// simulate find
setTimeout(function () {
cb(null, {
id: filter.id,
name: 'test',
role: 'test'
});
}, 500);
}
function savedb(obj, cb) {
//simulate db save
setTimeout(function () {
cb(null, obj);
}, 500);
}
// iterations count
var count = parseInt(process.argv[2], 10) || 3;
(function iterate(i) {
console.log(i);
if (i === count) {
// iterations complete
process.exit(1);
}
var data = {
id: 123 + i
};
smth(data, function (err, res) {
console.log(err || res);
iterate(++i);
});
})(0);
//make this follow async conventions with callback argument last
function smth(data, callback) {
//pseudocode database API here
db.save(data, function (error) {
if (error) {
callback(error);
return;
}
Object.findOne({ _id: ceva['id']}, function(obj) {
obj.save(callback);
});
});
}
That's the basic callback approach. You can use async.js if you like to clean it up a bit or study callbackhell.com for more ways to avoid the nested functions.

Check if user already exist and wait for callback

I have a small function to validate the user input. In that function I will also check if the email address is already taken. But I have some trouble with the async callback.
I hope anyone could give me a hint, how I could solve this. I use nodejs with the express 3 framework, mongodb and the node-validator library.
This is a part of my validation function:
function check(data, callback) {
mongoose.model('User', User).count(data, function (err, count) {
callback(err, !! count);
});
};
function validate(email, password, confirm) {
var v = new Validator(),
errors = new Array();
v.error = function (msg) {
errors.push(msg);
};
check({email: email}, function (err, exists) {
v.check(exists, { email: 'E-Mail is already taken' }).equals(false);
});
...
return errors;
}
Based on the async callback the variable errors is at the return statement empty. How could I else check if the email address is already in the databse?
You are going to have to add a callback to your validate function. As it stands now, your check function is asynch so you are immediately returning the empty errors array.
function check(data, callback) {
mongoose.model('User', User).count(data, function (err, count) {
callback(err, !! count);
});
};
function validate(email, password, confirm, callback) {
var v = new Validator(),
errors = new Array();
v.error = function (msg) {
errors.push(msg);
};
check({email: email}, function (err, exists) {
if (err) {
return callback(err);
}
v.check(exists, { email: 'E-Mail is already taken' }).equals(false);
callback(null, v);
});
};
// Calling to validate example
server.get("/validate_user_or_whatever", function(req, resp) {
// Call validate with callback
validate(req.param("email"), req.param("password"), true, function(err, validator) {
if (err) {
return resp.json({success: false, message: err.message});
}
var success = validator.isValid; // or something
// Respond with the results
resp.json({success: success});
});
});
Now, your next question is probably going to be how do I run all these validate functions and wait for them to return before calling the validate callback. Good question, take a look at the async module.
You need to think async and pass callbacks that will be called asynchronously.
Quick example:
app.get('/blah', function(req, res) {
// Whatever
validate(email, pass, confirm, function(errors) {
if (errors.length == 0)
resp.send(200, 'blah');
else
resp.send(400, 'blah blah');
});
})
function validate(email, password, confirm, callback) {
var v = new Validator(),
errors = new Array();
v.error = function (msg) {
errors.push(msg);
};
check({email: email}, function (err, exists) {
v.check(exists, { email: 'E-Mail is already taken' }).equals(false);
}, function() {
callback(errors);
});
// ...
return errors;
}

Resources