DiscoverMeteor/Microscope

Strange 'if' statement

methodbox opened this issue · 6 comments

Can you explain why, in section 'Meteor Method: Better Abstraction and Security' you don't close the 'if' statement?

Meteor.call('postInsert', post, function(error, result) {
      // display the error to the user and abort
      if (error)
        return alert(error.reason);
      Router.go('postPage', {_id: result._id});  
    });

I wrote this as:

Meteor.call('postInsert', post, function(error, result) {
      // display the error to the user and abort
      if (error){
        return alert(error.reason);
      Router.go('postPage', {_id: result._id});  
      }
    });

Which seems to accomplish the same. I'm unclear if this is something unique to Meteor methods or if I'm missing something big here.

It's just an alternative syntax for one-line if statements in JavaScript :)

methodbox, ... you don't need curly brackets around a single-line statement (the single statement after an if condition), but with what you wrote (with a curly bracket) the code will never execute your Router.go statement..

Okay yeah I figured this out later.

So I'm okay to use the syntax I used but my Router.go should be outside the if statement, correct?

So like this:

Meteor.call('postInsert', post, function(error, result) {
      // display the error to the user and abort
      if (error){
        return alert(error.reason);  
      }
    Router.go('postPage', {_id: result._id});
    });

yes

Awesome. Appreciate the responses guys. I like to stick with a consistent syntax even if not required.