另一个CoffeeScript错误 [英] Another CoffeeScript Error

查看:123
本文介绍了另一个CoffeeScript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我只是学习CoffeeScript和我继续得到错误。这是我的代码:

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

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

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

我得到这个错误: p>

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)

使用Expressjs和Nodejs with Native MongoDB Driver

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

推荐答案

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

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

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

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

打印 foo 。但是当使用对象语法 key:value 时,您定义了原型的属性(除了特殊关键字构造函数):

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)

长故事:使用而不是 = 时定义实例属性。 (对于静态属性, @a = b @a:b 是等价的; > UserDataProvider.a b 。)

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

这篇关于另一个CoffeeScript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆