CoffeeScript编译:意外的IF

时间:2014-02-25 21:28:38

标签: javascript coffeescript

我正在为API编写一些CoffeeScript代码,在我的代码的错误捕获部分中,我放置了一个IF语句。现在,在编译过程中,CoffeeScript说IF语句是意外的。

#  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

错误是

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

有没有人对我的代码中的问题有什么想法?

4 个答案:

答案 0 :(得分:7)

Unexpected 'INDENT' in CoffeeScript Example Code

此问题看起来有点类似。

因此请考虑检查编辑器中的标签和空格。

答案 1 :(得分:4)

另外需要注意的是括号和括号:

的Javascript

if (condition) {
    //logic
}

应该是CoffeeScript

if condition
    # logic
# END if

答案 2 :(得分:1)

注意长条件缩进,例如:

if condition and other_condition and
yet_another_condition
^^

应该是

if condition and other_condition and
  yet_another_condition

例如,Intellij打破了这个缩进

答案 3 :(得分:0)

我的问题是我做了以下事情:

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

问题是箭头函数不能有大括号,除非返回一个对象。我的解决方案是去掉大括号:

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