javascript - CoffeeScript + Express.js : cannot call method 'sliced' of undefined

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

最佳答案

您永远不会关闭 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 的好处之一是您可以使用缩进,而不需要像源代码中的右括号那样有大量的 float 结束定界符。只要这样做:

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

关于javascript - CoffeeScript + Express.js : cannot call method 'sliced' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17679817/

相关文章:

javascript - Lodash 的纯 JavaScript 替换 `omit()`

node.js - 路径在 tsconfig.app.json 中没有按预期工作

node.js - 如何在不使用排序的情况下检索 mongodb-mongoose 中最近添加的文档?

node.js - Express 和子域(无静态文件)

javascript - 从本地主机向远程服务器发出请求,获取预检请求 'Access-Control-Allow-Origin'

node.js - socket.io 授权引发 "handshake error"

javascript - 将联系表单信息存储到 JavaScript (ES6) 中的类中?

javascript - 由于数据库请求(门户字段)显示 <div>

JavaScript 数组不返回数字

Nodejs 中的 Java 密码