将module.exports分配给函数返回未定义的方法

时间:2014-06-02 23:02:10

标签: node.js coffeescript

我正在整合breeze.js尝试将此文件(github)复制到coffeescript并在我的项目中使用它。正下方是我的翻译,然后我设置了module.exports

d_b = (database) ->
    MongoClient = require('mongodb').MongoClient
    db = null

    mongoURL = "mongodb://localhost/project_db"
    mongoOptions = server: {auto_reconnect: true}

    getDB = (next) ->
        console.log 'getDB()'
        if db
            next null, db

        else
            MongoClient.connect mongoUrl, mongoOptions, (err, project_db) ->
                if err
                    err.message = (err.message or '') + '\nIs the MongoDb server running?'
                    next err
                else
                    db = project_db
                    next null, db

    database.getDB = getDB
    return

module.exports = d_b

当我使用它时,我收到错误。

# in another file
database = require '../database'
database.getDB (err, db) ->
    # ...

TypeError: Object function (database) {
    var MongoClient, db, getDB, mongoOptions, mongoURL;
    MongoClient = require('mongodb').MongoClient;
    db = null;
    mongoURL = "mongodb://localhost/project_db";
    mongoOptions = {
      server: {
        auto_reconnect: true
      }
    };
    getDB = function(next) {
      console.log('getDB()');
      if (db) {
        return next(null, db);
      } else {
        return MongoClient.connect(mongoUrl, mongoOptions, function(err, project_db) {
          if (err) {
            err.message = (err.message || '') + '\nIs the MongoDb server running?';
            return next(err);
          } else {
            db = utourney_db;
            return next(null, db);
          }
        });
      }
    };
    database.getDB = getDB;
  } has no method 'getDB'

1 个答案:

答案 0 :(得分:1)

您移植的原始代码是一个立即调用的函数,调用module.exports作为第一个参数。因此,更直接的翻译将是:

((database) ->
    MongoClient = require('mongodb').MongoClient
    db = null

    mongoURL = "mongodb://localhost/project_db"
    mongoOptions = server: {auto_reconnect: true}

    getDB = (next) ->
        console.log 'getDB()'
        if db
            next null, db

        else
            MongoClient.connect mongoUrl, mongoOptions, (err, project_db) ->
                if err
                    err.message = (err.message or '') + '\nIs the MongoDb server running?'
                    next err
                else
                    db = project_db
                    next null, db

    database.getDB = getDB
    return
)(module.exports)

在CoffeeScript中,我可能更像这样编写模块:

MongoClient = require('mongodb').MongoClient
db = null

mongoURL = "mongodb://localhost/project_db"
mongoOptions = server: {auto_reconnect: true}

module.exports = (next) ->
    console.log 'getDB()'
    if db
        next null, db

    else
        MongoClient.connect mongoUrl, mongoOptions, (err, project_db) ->
            if err
                err.message = (err.message or '') + '\nIs the MongoDb server running?'
                next err
            else
                db = project_db
                next null, db

然后你会像这样使用它:

database = require '../database'
database (err, db) ->
  # ...