另一个 CoffeeScript 错误

发布于 2024-10-19 10:40:37 字数 2703 浏览 1 评论 0原文

嘿,我刚刚学习 CoffeeScript,但一直出错。这是我的代码:

Db   = require('./lib/mongodb').Db
ObjectID = require('./lib/mongodb').ObjectID
Server   = require('./lib/mongodb').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)
    findAll = (callback) ->
        this.getCollection (error,data)->
            if error then callback error
            else
                data.find (error, cursor) ->
                    if error then callback error
                    else
                        cursor.toArray (error, results)->
                            if error then callback error
                            else callback(null,results)
    findById = (id,callback)->
        this.getCollection (error, data)->
          if error then callback error
          else
            data.findOne { _id: id} , (error, result)->
              if error then callback error
              else callback(null, result)
    save = (data, callback)->
        this.getCollection (error, collection)->
            if error then callback error
            else
                if typeof(data.length) is "undefined"
                then data = [data]

                collection.insert data ()->
                    callback null, data

exports.UserDataProvider = UserDataProvider

当我尝试使用 userdataprovider.save(??BLAH BLAH BLAH ??) // 我已经实例化了它。

我收到此错误:

TypeError: Object #<UserDataProvider> has no method 'save'
    at Object.<anonymous> (/home/akshay/dev/statServer/app.js:8:15)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:157:15)
    at pass (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:162:10)
    at Object.router [as handle] (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:168:6)
    at next (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/index.js:218:15)
    at Object.handle (/usr/local/lib/node/.npm/express/1.0.7/package/lib/express/server.js:65:5)
    at next (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/index.js:218:15)
    at Server.handle (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/index.js:231:3)
    at Server.emit (events.js:45:17)

如果重要的话,我将 Expressjs 和 Nodejs 与本机 MongoDB 驱动程序一起使用

Hey, I am Just Learning CoffeeScript and I Keep getting Errors. Here is my code:

Db   = require('./lib/mongodb').Db
ObjectID = require('./lib/mongodb').ObjectID
Server   = require('./lib/mongodb').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)
    findAll = (callback) ->
        this.getCollection (error,data)->
            if error then callback error
            else
                data.find (error, cursor) ->
                    if error then callback error
                    else
                        cursor.toArray (error, results)->
                            if error then callback error
                            else callback(null,results)
    findById = (id,callback)->
        this.getCollection (error, data)->
          if error then callback error
          else
            data.findOne { _id: id} , (error, result)->
              if error then callback error
              else callback(null, result)
    save = (data, callback)->
        this.getCollection (error, collection)->
            if error then callback error
            else
                if typeof(data.length) is "undefined"
                then data = [data]

                collection.insert data ()->
                    callback null, data

exports.UserDataProvider = UserDataProvider

When I try to use
userdataprovider.save( ?? BLAH BLAH BLAH ??) // I already instantiated it.

I get this error:

TypeError: Object #<UserDataProvider> has no method 'save'
    at Object.<anonymous> (/home/akshay/dev/statServer/app.js:8:15)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:157:15)
    at pass (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:162:10)
    at Object.router [as handle] (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:168:6)
    at next (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/index.js:218:15)
    at Object.handle (/usr/local/lib/node/.npm/express/1.0.7/package/lib/express/server.js:65:5)
    at next (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/index.js:218:15)
    at Server.handle (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/index.js:231:3)
    at Server.emit (events.js:45:17)

If it matters, I am using Expressjs and Nodejs with Native MongoDB Driver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-10-26 10:40:37

问题是您使用 = 而不是 : 来定义实例级方法。

CoffeeScript 的class 构造是对象和函数的奇怪混合体。类体内的代码立即运行,例如,

class UserDataProvider
    a = 'foo'
    console.log a

打印 foo。但是,当您使用对象语法key: value时,您定义原型的属性(特殊关键字constructor的情况除外):

class UserDataProvider
  a: 'foo'

(new UserDataProvider).a   # 'foo'

这可能看起来很奇怪,但它允许您运行有用的一次性静态初始化代码,包括只能由类中定义的方法看到的私有变量:

class UserDataProvider
    secretPassword = Math.random()
    getHash: -> hash(secretPassword)

长话短说:使用 : 而不是 =< /code> 定义实例属性时。 (对于静态属性,@a = b@a: b 是等效的;都将 UserDataProvider.a 设置为 b代码>.)

The problem is that you're using = instead of : to define instance-level methods.

CoffeeScript's class construct is an odd hybrid of object and function. Code within the class body runs immediately—for instance,

class UserDataProvider
    a = 'foo'
    console.log a

prints foo. But when you use the object syntax key: value, you define properties of the prototype (except in the case of the special keyword constructor):

class UserDataProvider
  a: 'foo'

(new UserDataProvider).a   # 'foo'

This might seem odd, but it allows you to run one-time static initialization code which can be useful, including private variables that can only be seen by methods defined within the class:

class UserDataProvider
    secretPassword = Math.random()
    getHash: -> hash(secretPassword)

Long story short: Use : instead of = when defining instance properties. (For static properties, @a = b and @a: b are equivalent; both set UserDataProvider.a to b.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文