最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CoffeeScript compiling: Unexpected IF - Stack Overflow

programmeradmin3浏览0评论

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 pilation 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?

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 pilation 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?

Share Improve this question edited Feb 25, 2014 at 21:35 TechnoCF asked Feb 25, 2014 at 21:28 TechnoCFTechnoCF 1781 silver badge8 bronze badges 2
  • I have fixed the issue with my errors by using a different method to handle the errors. – TechnoCF Commented Feb 25, 2014 at 21:38
  • @techo this was probably an indention error. – agconti Commented Feb 25, 2014 at 21:39
Add a ment  | 

4 Answers 4

Reset to default 7

Unexpected 'INDENT' in CoffeeScript Example Code

This issue looks somehow similar.

Consider therefore checking tabs and spaces in your editor.

Another thing to watch out for parenthesis and braces:

Javascript

if (condition) {
    //logic
}

should be CoffeeScript

if condition
    # logic
# END if

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

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 {}
发布评论

评论列表(0)

  1. 暂无评论