TechnoCF
TechnoCF

Reputation: 178

CoffeeScript compiling: Unexpected IF

I am writing some CoffeeScript code for an API, and in the error catching section of my code I placed an IF statement. Now, during the compilation process CoffeeScript is saying that the IF statement was unexpected.

#  Handle Errors
app.error (err, req, res, next) ->
    if err instanceof NotFound
        res.send '404, not found.'
    else
        res.send '500, internal server error.'

app.get '/*', (req, res) ->
    throw new NotFound

NotFound = (msg) ->
    this.name = 'NotFound'
    Error.call this, msg
    Error.captureStackTrace this, arguments.callee

The error is

/home/techno/node/snaprss/application.coffee:22:5: error: unexpected if
    if err instanceOf NotFound
    ^^

Does anyone have any ideas where the issue is within my code?

Upvotes: 7

Views: 9616

Answers (4)

Saiansh Singh
Saiansh Singh

Reputation: 667

My problem was that I did the following:

myArray.map (line) -> {
  if a equals ''
    return {}
}

The problem was that an arrow function cannot have braces unless returning an object. My solution was to remove the braces:

myArray.map (line) ->
  if a equals ''
    return {}

Upvotes: 0

Jake Robers
Jake Robers

Reputation: 59

Another thing to watch out for parenthesis and braces:

Javascript

if (condition) {
    //logic
}

should be CoffeeScript

if condition
    # logic
# END if

Upvotes: 4

davide
davide

Reputation: 316

Watch out for long conditions indentation for example:

if condition and other_condition and
yet_another_condition
^^

it should be

if condition and other_condition and
  yet_another_condition

For example Intellij breaks this indentation

Upvotes: 0

dnl-blkv
dnl-blkv

Reputation: 2128

Unexpected 'INDENT' in CoffeeScript Example Code

This issue looks somehow similar.

Consider therefore checking tabs and spaces in your editor.

Upvotes: 7

Related Questions