无法使用Node.js + Expressjs连接到MongoHQ

时间:2012-12-11 17:52:25

标签: node.js mongodb heroku

我最近一直在努力学习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

对可能导致问题的任何帮助都将受到高度赞赏。

0 个答案:

没有答案