Will someone please review this code for ‘correctness’?
@serkandurusoy, I was reading over some of your stack overflow and aldeed repo posts, will this work, since the client is async (it’s async even without a callback)?
client.js
'client #did-something': function (event, instance) {
var some_value = Meteor.find('#some-value');
try {
Meteor.call('do_something', some_value, function (err, return_value) {
if (err) throw new Meteor.Error('bubble up this error', err);
else Session.set('got_it', return_value);
});
// do other stuff here
} catch (e) {
toastr.error(e);
console.log('error bubbled up from do_something method call');
console.log(e);
}
}
lib/methods.js
// SIMULATED server method call
do_something: function (some_value) {
// do stuff
var documentId = ''
// unblock so the client continues?
this.unblock();
try {
documentId = good_collection.insert(some_value);
// do something here that takes a long time, then return to the client
} catch (e) {
throw new Meteor.Error(555, e);
}
)
If an error gets thrown in the method, will the exception bubble up to the try/catch in the client?
And on the publish/subscribe side, does the subscribe callback get the error from the publish, is this the ‘right’ way?
client.js
Template.template_name.onCreated(function() {
var instance = this;
instance.autorun(function() {
var subscription = instance.subscribe('get_collection', function(err) {
if (err) {
console.log('error from the publish,', err);
}
});
if (subscription.ready()) {
console.log("received user cursor. \n\n")
} else {
console.log("subscription is not ready yet. \n\n");
}
});
instance.the_collection = function() {
var obj = my_collection.findOne({
_id: Session.get('session_id')
});
return obj;
}
});
should we use this.error here, instead of Meteor.Error?
server.js
Meteor.publish('get_collection', function() {
var self = this;
if (self.userId) {
try {
return my_collection.find({});
} catch (e) {
this.error(555, 'publish error');
}
}
return self.ready();
});