我最近一直在尝试同时学习 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
任何可能导致问题的帮助将不胜感激。