Meteor pattern for multiple method calls sequential callbacks - javascript

I keep running into this pattern when coding in Meteor where I find myself making multiple method calls nested within each other - first method fires, then in the callback, a second one fires which is dependent on the first one's result, etc. Is there a better pattern for using multiple methods without nested method calls inside callbacks? The code quickly gets messy.
Meteor.call('unsetProduct', product._id, omitObj, function(err, result) {
if(!err) {
Meteor.call('editProduct', product._id, object, function(err, result) {
if(!err) {
//if no error, then continue to update the product template
Meteor.call('editProductTemplate', self._id, obj, function(err, result) {
if(!err) {
//call some other method
}
else {
FormMessages.throw(err.reason, 'danger');
}
});
}
else {
FormMessages.throw(err.reason, 'danger');
}
});//end edit product
}
else {
AppMessages.throw(err.reason, 'danger');
}
});`

Take a look at reactive-method package. I think it does exactly what you need: it wraps asynchronous Meteor.calls into synchronous code. With it, your code would look cleaner, like
try {
const result = ReactiveMethod.call('unsetProduct', product._id, omitObj);
} catch (error) {
AppMessages.throw(err.reason, 'danger');
}
try {
const nestedResult = ReactiveMethod.call('editProduct', product._id, object);
} catch (error) {
FormMessages.throw(err.reason, 'danger');
}
try {
const evenMoreNestedResult = ReactiveMethod.call('editProductTemplate', self._id, obj);
} catch (error) {
FormMessages.throw(err.reason, 'danger');
}
Which will look nicer when you add some logic inside try statements.

Related

node.js : How do I pass in multiple variables into a view?

I'm trying to create a webpage where there is an instance of all the current Projects I am working on, on the left, so I'd need a .forEach() function in order to loop through all of them in order to display it, but on the other side, I need to display the information that is currently selected.
Please first take a look at my code block so I can try to explain the thought process behind what I was trying to do.
So I didn't have any problems selecting the information of the single project that I needed to display on this webpage. I used the .findOne() function in order to pick out the information that I needed.
The problem that I'm facing is that I also need to pass a var that's connected to the .find() function in order to pass through all of the elements of the database. The way I went about this is that I thought I would be able to set the definition of allProjects by manually running the .find() function, and then returning it, thus assigning Projects.find() to allProjects.
app.get('/projects/:url', (req, res) => {
Projects.findOne({ Url: req.params.url }, (err, foundProject) => {
if (err) {
console.log(err);
} else {
res.render('show', {
foundProject: foundProject,
allProjects: Projects.find({}, (err, allProjects) => {
if (err) {
res.send('error');
} else {
return allProjects;
}
})
});
}
});
});
I thought that by returning allProjects and then also having that assigned to allProjects, i'd be able to use the allProjects variable in my show.ejs page.
Unfortunately, I'm getting an error 'allProjects.forEach() is undefined' which leads me to believe that in the app.js where I am defining allProjects, it's not being assigned the correct value that I want it assigned.
It looks like you're expecting return allProjects to do something, but that's actually ignored. Unless you have a callback function you can call, that will go into the void and never be seen by anyone. This is true of virtually all callback functions. They do not care what value that function returns because it's never relevant, what they want is the future value which comes through the callback given to this function.
In other words it plays out like this:
asyncFunctionTakingCallback(function(cb) {
cb(null, value); // This is the important value!
return value; // Nobody cares about this value. Don't even bother.
});
To fix that you need to move the render call inside of the inner-most callback function:
app.get('/projects/:url', (req, res) => {
Projects.findOne({ Url: req.params.url }, (err, foundProject) => {
if (err) {
console.log(err);
// Return here to avoid another level of indentation below
return;
}
Projects.find({}, (err, allProjects) => {
if (err) {
res.send('error');
} else {
res.render('show', {
foundProject: foundProject,
allProjects:
});
}
});
});
});
Now that's still a dizzying amount of code and the nesting here is getting completely out of control even though this is relatively simple Node code.
For comparison here's a version that uses async functions:
app.get('/projects/:url', async (req, res) => {
let foundProject = await Projects.findOne({ Url: req.params.url });
res.render('show', {
foundProject: foundProject,
allProjects: await Projects.find({})
});
});
There's really not much to it this way. What await does is basically stall out on that line and wait for the promise to get resolved or produce an error. Any errors produced should be captured with try { ... } catch as usual.

Conditional Callbacks Execution : What would be an elegant way to solve it?

I have created an almost unreadable code like this with different MongoDB queries. There is a high chance you can't make it till the end.
Collection1.find({}, (err, doc) => {
if (err)
throw err;
else if (doc) {
Collection2.find({}, (err, doc) => {
if (err)
throw err;
else if (doc) {
let cursor = Collection3.find({})
cursor.exec((err, doc) => {
if (err)
throw err;
else if (doc) {
Collection4.find();
}
});
} else {
var y = new Collection2({
x: "S",
y: "D"
})
y.save((err, doc) => {
if (err)
throw err;
else if (doc) {
Collection3.find({});
let cursor = Collection3.find({})
cursor.exec((err, doc) => {
if (err)
throw err;
else if (doc) {
Collection4.find();
}
})
}
})
}
})
} else {
taskComplete();
}
})
Now one solution to the callback hell problem is that I break those anonymous function as named function.That might solve some problem but I think we can do better than that.
I also thought about async.series, async.waterfall... Somehow I feel they don't fit the bill as it's not like I need to get this task done post this. It's more like if this than that which is not supported by both of these. I cannot choose which task to run based on conditions.
Which leaves me with promises and generators.
Well, those DB calls are promises in itself. But how to integrate that with if and else is something I don't have experience with. I was hoping that more experienced JavaScript out here on SO could help me out with this and give me some guidelines on how to handle this issue more gracefully.
Edit I haven't really given a thought to generators. But would they help if included?

How to write this code in async/callbacks in nodejs?

I am trying to perform SQL queries based on the callback results in if conditions but I am unable to write the code. So can somebody please provide some information how to do this with async/callback methods?
app.get('/resell-property', function(req, res) {
var data = {}
data.unit_price_id = 1;
function callback(error, result) {
if (result.count == 0) {
return hp_property_sell_request.create(data)
} else if (result.count > 0) {
return hp_unit_price.findAll({
where: {
unit_price_id: data.unit_price_id,
hp_property_id: data.property_id,
hp_unit_details_id: data.unit_details_id
}
})
}
}
hp_property_sell_request.findAndCountAll({
where: {
unit_price_id: data.unit_price_id
}
}).then(function (result) {
if (result) {
callback(null, result);
}
});
});
How can I write the callbacks for this?
hp_property_sell_request.create(data) ,hp_unit_price.findAll({
where: {
unit_price_id: data.unit_price_id,
hp_property_id: data.property_id,
hp_unit_details_id: data.unit_details_id
}
})
After returning the result I want to handle callbacks and perform this query:
if (result.request_id) {
return hp_unit_price.findAll({
where: {
unit_price_id:result.unit_price_id,
hp_property_id:result.property_id,
hp_unit_details_id:result.unit_details_id
}
}).then(function(result) {
if (result.is_resale_unit==0 && result.sold_out==0) {
return Sequelize.query('UPDATE hp_unit_price SET resale_unit_status=1 WHERE hp_unit_details_id='+result.unit_details_id+' and hp_property_id='+result.property_id)
}
})
}
Your question is too vague to answer precisely. I am assuming you need to call two methods and the latter should be called after the first method is completed and you need the results from the first method available in second methods.
Now, to the solution
Please note that callback and promise are very different approaches. It is very wise to stick with only one and not to mix callbacks and promises.
For your requirement, I highly recommend promises to avoid
callback hell.
Using callbacks, the solution looks something like
app.get('/resell-property', function(req, res) {
method1(req.body.id,function(err,result){
return method2(result);
})
function method1(input1,callback(err,result)){
try{
var result=st.execute("query");
return callback(null,result)
}catch(error){
return callback(error,null);
}
}
function method2(input2){
return input2;
}
})

Mongoose Find Results and Async

I am writing a NodeJS script that will run every hour through Heroku's scheduler. I am quering the Mongo instance I have (mongohq/compose) and then doing something with those results. I am working with Mongoose.js and the find() command. This returns an array of results. With those results I need to perform additional queries as well as some additional async processing (sending email, etc).
Long story short, due to node's async nature I need to wait until all the processing is complete before I call process.exit(). If I do not the script stops early and the entire result set is not processed.
The problem is that I have a christmas tree effect of calls at this point (5 nested asnyc calls).
Normally I'd solve this with the async.js library but I'm having a problem seeing this through with this many callbacks.
How can I make sure this entire process finishes before exiting the script?
Here's the code that I'm working with (note: also using lodash below as _):
Topic.find({ nextNotificationDate: {$lte: moment().utc()}}, function (err, topics) {
if (err) {
console.error(err);
finish();
} else {
_.forEach(topics, function (topic, callback) {
User.findById(topic.user, function (err, user) {
if (err) {
// TODO: impl logging
console.error(err);
} else {
// Create a new moment object (not moment.js, an actual moment mongoose obj)
var m = new Moment({ name: moment().format("MMM Do YY"), topic: topic});
m.save(function(err) {
if(err) {
// TODO: impl logging
console.error(err);
} else {
// Send an email via postmark
sendReminderTo(topic, user, m._id);
// Update the topic with next notification times.
// .. update some topic fields/etc
topic.save(function (err) {
if(err) {
console.error(err);
} else {
console.log("Topic updated.");
}
})
}
})
}
});
console.log("User: " + topic.user);
});
}
});
Part of what is making your code confusing is the usage of else statements. If you return your errors, you won't need the else statement and save 4 lines of indentation for every callback. That in and of itself will make things drastically more readable.
Using async:
Topic.find({nextNotificationDate: {$lte: moment().utc()}}, function (err, topics) {
if (err) {
console.error(err);
return finish(err);
}
async.each(topics, function(topic, topicCallback) {
async.auto({
user: function (callback) {
User.findById(topic.user, callback);
},
moment: function(callback) {
var m = new Moment({name: moment().format("MMM Do YY"), topic: topic});
m.save(callback);
},
topic: ["moment", "user", function (callback, results) {
var m = results.moment;
var user = results.user;
sendReminderTo(topic, user, m._id);
topic.save(callback);
}]
}, function(err) {
if (err) {
return topicCallback(err);
}
console.log("Topic updated.")
return topicCallback();
});
}, function(err) {
if (err) {
console.error(err);
return finish(err);
}
return finish();
});
});

Using node js and redis - get readable code

May be this is simple and stupid question, but im just learning my first asynchronous server language and redis is my first key-value db.
Example. I need do this:
$x = users:count
get user:$x
But with asynchronic javascript i get this code
redis-cli.get('users:count', function(err, repl){
if(err){
errorHandler(err);
} else {
redis-cli.get('user:'+repl, function(err, repl){
if(err){
errorHandler(err);
} else {
console.log('We get user '+repl+'!')
}
})
}
})
This code not so large and not nested to much, but it's look like on my firt not example/test project i get crazy nested functions-callbacks.
How solve this and make pretty and readable code?
function getUserCount(callback, err, repl) {
if (err) {
return callback(err);
}
redis-cli.get('user:' + repl, getUser.bind(null, callback));
}
function getUser(callback, err, repl) {
if (err) {
return callback(err);
}
console.log('We get user ' + repl + '!');
}
redis-cli.get('users:count', getUserCount.bind(null, errorHandler));
bind works wonders. If you prefer to have the bind abstracted then you can use this to store state that would normally be stored in closures like:
require("underscore").bindAll({
run: function (errorHandler) {
this.errorHandler = errorHandler;
redis-cli.get('users:count', this.getUserCount);
},
getUserCount: function (err, repl) {
if (err) return this.errorHandler(err);
redis-cli.get('user:' + repl, this.getUser);
},
getUser: function (err, repl) {
if (err) return this.errorHandler(err);
console.log('got user ', repl);
}
}).run(errorHandler);

Categories