【问题标题】:Unable to connect to MongoHQ using Node.js + Expressjs无法使用 Node.js + Expressjs 连接到 MongoHQ
【发布时间】:2012-12-19 07:37:14
【问题描述】:

我最近一直在尝试同时学习 Node.js 和 Mongo。我在 Heroku 上设置了一个帐户,让一个简单的 Node.js 应用程序正常运行,然后潜入了 Mongo。我在 MongoHQ 上添加了,但从那以后一直无法将应用程序连接到数据库。每次我运行应用程序时它都会崩溃。在这一点上,我只想弄清楚我的数据库连接或我对 mongoskin 的使用有什么问题。这是我当前的代码:

var express = require('express');
var mongo = require('mongoskin');

var app = express.createServer(express.logger());

var conn = mongo.db(process.env.MONGOHQ_URL);

app.get('/', function(request, response) {
    conn.collection('facts').find().toArray(function(err, facts){
            if(err) throw err;
            response.send(facts);
    });

});

var port = process.env.PORT || 5000;
app.listen(port, function() {
  console.log("Listening on " + port);
});

还有我的包文件:

{
    "name": "app1",
    "version": "0.0.4",
    "dependencies": {
            "express": "3.x",
            "mongoskin" : "0.1.x"
    },
    "engines": {
            "node": "0.8.x",
            "npm": "1.1.x"
    }
}

最后是错误代码:

2012-12-11T17:42:32+00:00 app[web.1]:     at Module.load (module.js:356:32)
2012-12-11T17:42:32+00:00 app[web.1]:     at Module._compile (module.js:449:26)
2012-12-11T17:42:32+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/mongoskin/lib/mongoskin/db.js:162:28)
2012-12-11T17:42:32+00:00 app[web.1]:     at Db.Object.defineProperty.get [as state] (/app/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/db.js:2125:31)
2012-12-11T17:42:32+00:00 app[web.1]:     at Module.require (module.js:362:17)
2012-12-11T17:42:32+00:00 app[web.1]:     at Module._compile (module.js:449:26)
2012-12-11T17:42:32+00:00 app[web.1]: TypeError: Cannot read property '_serverState' of undefined
2012-12-11T17:42:32+00:00 app[web.1]:     at Function.Module._load (module.js:312:12)
2012-12-11T17:42:32+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/mongoskin/lib/mongoskin/index.js:2:10)
2012-12-11T17:42:32+00:00 app[web.1]: 
2012-12-11T17:42:32+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:467:10)
2012-12-11T17:42:32+00:00 app[web.1]:     at require (module.js:378:17)
2012-12-11T17:42:33+00:00 heroku[web.1]: Process exited with status 1
2012-12-11T17:42:33+00:00 heroku[web.1]: State changed from starting to crashed

对于可能导致问题的任何帮助,我们将不胜感激。

【问题讨论】:

    标签: node.js mongodb heroku


    【解决方案1】:

    您只需更新mongoskin 库。替换这一行:

    "mongoskin" : "0.1.x"

    "mongoskin" : ">= 0.3.6"

    更多详情here

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 2011-11-26
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 2013-01-22
      相关资源
      最近更新 更多