javascript-第6行(CoffeeScript)编译器错误未封闭的INDENT

这是我在CoffeeScript中使用MongoDB进行简单测试的代码.
当我跑步
  coffee -c UserDataProvider.coffee
我在第6行出现错误UNCLOSED INDENT

这是我的代码:

Db   = require('mongodb/db').Db
ObjectID = require('mongodb/bson/bson').ObjectID
Server   = require('mongodb/connection').Server

class UserDataProvider
    constructor = (host,port)->
        this.db = new Db( 'test' , new Server(host ,port,{}))

    getCollection = (callback) ->
        this.db.collection('data',(error,data)->
            if error then callback(error)
            else callback(data)

StackTrace是:

Error: In UserDataProvider.coffee, unclosed INDENT on line 6
    at Error (unknown source)
    at Rewriter.ensureBalance (/usr/local/lib/node/.npm/coffee-script/1.0.1/package/lib/rewriter.js:283:17)
    at Rewriter.rewrite (/usr/local/lib/node/.npm/coffee-script/1.0.1/package/lib/rewriter.js:21:12)
    at Lexer.tokenize (/usr/local/lib/node/.npm/coffee-script/1.0.1/package/lib/lexer.js:37:29)
    at Object.compile (/usr/local/lib/node/.npm/coffee-script/1.0.1/package/lib/coffee-script.js:26:34)
    at /usr/local/lib/node/.npm/coffee-script/1.0.1/package/lib/command.js:117:33
    at /usr/local/lib/node/.npm/coffee-script/1.0.1/package/lib/command.js:85:26
    at [object Object].<anonymous> (fs.js:86:5)
    at [object Object].emit (events.js:39:17)
    at afterRead (fs.js:843:12)

有人可以帮忙吗?

解决方法:

这行的第一个左括号永远不会关闭:

this.db.collection('data',(error,data) ->

实际上根本不需要它,因此,如果您打算将“数据”作为第一个参数,并将function(error,data){…}作为第二个参数,则您可能希望将其全部删除.

上一篇:为什么Atom文档使用CoffeeScript,却用JavaScript生成软件包?


下一篇:javascript – .bind函数与CoffeeScript