【问题标题】:Coffeescript + Express.js : cannot call method 'sliced' of undefinedCoffeescript + Express.js:无法调用未定义的“切片”方法
【发布时间】:2013-07-14 19:51:15
【问题描述】:

我正在用 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 有关,但想不出任何东西。有什么想法吗?

【问题讨论】:

  • 看起来像是咖啡脚本内部的错误。你运行的是最新版本吗?
  • 是的,coffeescript(1.6.3) 和 express(3.3.4) 的最新版本。它在一个简单的 hello world 中运行良好(当我注释掉对 contacts.coffee 的调用时)

标签: javascript node.js express coffeescript slice


【解决方案1】:

您永远不会关闭 new ContactModel( 的括号。

  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)
)

我的提示:忘记所有这些括号。关于 coffeescript 的部分好处是您可以使用缩进,而不是像源代码中的右括号那样有大的浮动关闭分隔符链。做吧:

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

【讨论】:

  • 谢谢你!知道为什么coffeescript 不只是告诉我这是一个paren 问题吗?
猜你喜欢
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 2023-03-16
  • 2020-01-19
  • 1970-01-01
  • 2015-06-18
  • 2013-09-25
  • 1970-01-01
相关资源
最近更新 更多