Coffeescript + Express.js:无法调用未定义的方法'切片'

时间:2013-07-16 14:47:45

标签: javascript node.js express coffeescript slice

我在express.js和coffeescript中创建了我的第一个应用程序。代码位于: https://github.com/findjashua/contactlist

当我尝试运行它时,我收到以下错误:

/Users/jashua/local/lib/node_modules/coffee-script/lib/coffee-script/helpers.js:216
  codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end
                      ^
TypeError: Cannot call method 'slice' of undefined
  at Object.exports.prettyErrorMessage (/Users/jashua/local/lib/node_modules/coffee-script/lib/coffee-script/helpers.js:216:27)
  at compileScript (/Users/jashua/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:199:25)
  at /Users/jashua/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:143:18
  at fs.js:266:14
  at Object.oncomplete (fs.js:107:15)

我假设它与contacts.coffee有关,但无法想到任何事情。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你永远不会关闭new ContactModel(的parens。

  exports.addContact = (req, res) ->
contact = new ContactModel(
    name: req.body.name
    phone: req.body.phone
contact.save((err) ->
    if not err
        console.log('created')
        res.send(contact)
    else
        res.send(error)
)

我的提示:忘记所有这些parens。关于coffeescript的一个好处是你可以使用缩进而不是像你的源代码中关闭的parens那样有大的浮动关闭分隔符链。只是做:

someFunction (arg1, arg2) ->
  body of nested function